leetcode145. Binary Tree Postorder Traversal

Posted seyjs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode145. 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

 

以上是关于leetcode145. Binary Tree Postorder Traversal的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 145. Binary Tree Postorder Traversal

LeetCode 145:Binary Tree Postorder Traversal

leetcode145. Binary Tree Postorder Traversal

LeetCode 145: Binary Tree Postorder Traversal

leetcode No145. Binary Tree Postorder Traversal

LeetCode OJ 145. Binary Tree Postorder Traversal