【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

原文地址:https://www.cnblogs.com/seyjs/p/9399279.html

时间: 2024-10-31 09:08:42

【leetcode】145. Binary Tree Postorder Traversal的相关文章

【LeetCode】145. Binary Tree Postorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51494797 Subject 出处:https://leetcode.com/problems/binary-tree-postorder-traversal/ Hard 级别 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree

【LeetCode】94. Binary Tree Inorder Traversal

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? OJ's Binary Tree Serialization: The seriali

【LeetCode】144. Binary Tree Preorder Traversal

题目: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 提示: 首先需要明确前序遍历的顺序,即:节点 -> 左孩子 -> 右孩子,这一顺序

<LeetCode OJ> 145. Binary Tree Postorder Traversal

在此之前回顾前序遍历和中序遍历: 1,前序遍历: 基本规则,总是先访问根节点在左节点,在右节点 递归解法: class Solution { public: vector<int> result; vector<int> preorderTraversal(TreeNode* root) { if(root){ result.push_back(root->val); preorderTraversal(root->left); preorderTraversal(ro

【leetcode】Construct Binary Tree from Preorder and Inorder Traversal

问题: 给定二叉树的前序和中序遍历,重构这课二叉树. 分析: 前序.中序.后序都是针对于根结点而言,所以又叫作先根.中根.后根(当然不是高跟). 前序:根  左 右 中序:左  根 右 对二叉树,我们将其进行投影,就会发现个有趣的事: 发现投影后的顺序,恰好是中序遍历的顺序,这也就是为什么在构造二叉树的时候,一定需要知道中序遍历,因为中序遍历决定了结点间的相对左右位置关系.所以,对一串有序的数组,我们可以来构建二叉有序数,并通过中序遍历,就可以得到这个有序的数组. 既然中序遍历可以通过根结点将序

【LeetCode】103. Binary Tree Zigzag Level Order Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51524241 Subject 出处:https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ri

LeetCode解题报告:Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 注意:下面是迭代的解法.理解有点困难,和大家讨论一下. 1 import java.uti

【LeetCode】Construct Binary Tree from Inorder and Postorder Traversal 解题报告

[题目] Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. [解析] 题意:根据二叉树中序遍历和后序遍历的结果,构造该二叉树. 首先明确一下,中序遍历顺序:left - root - right,后序遍历顺序:left  - right - root. 很显然,后序遍历的

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur