【leetcode】145. Binary Tree Postorder Traversal

题目如下:

解题思路:凑数题+3,搞不懂为什么本题的难度是Hard,而【leetcode】590. N-ary Tree Postorder Traversal是Medium。

代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def postorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if root == None:
            return []
        res = []
        stack = [root]
        while len(stack) > 0:
            node = stack.pop(0)
            res.insert(0,node.val)
            if node.left != None:
                stack.insert(0,node.left)
            if node.right != None:
                stack.insert(0,node.right)
        return res

 

posted @ 2018-08-01 08:29  seyjs  阅读(114)  评论(0编辑  收藏  举报