145. Binary Tree Postorder Traversal QuestionEditorial Solution

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?

每次都先push right, 然后left。 但是需要一个sentinel存每次上一次pop的node, 如果这个node是新的peek的值的子数的话,不能再push到stack,需要进入输出逻辑。 同样进入输出逻辑的还有leaf。

    public IList<int> PostorderTraversal(TreeNode root) {
        var res = new List<int>();
        var stack = new Stack<TreeNode>();
        if(root == null) return res;
        stack.Push(root);
        var rightPointer = new TreeNode(-1);
        while(stack.Count() > 0)
        {
            if((root.right == null && root.left == null)||(root.left == rightPointer ) || (root.right == rightPointer) )
            {
                res.Add(root.val);
                rightPointer = stack.Pop();
                if(stack.Count()>0)
                root = stack.Peek();
            }
            else
            {
                if(root.right != null) stack.Push(root.right);
                if(root.left != null) stack.Push(root.left);
                root = stack.Peek();
            }

        }
        return res;
    }
时间: 2024-08-10 23:29:35

145. Binary Tree Postorder Traversal QuestionEditorial Solution的相关文章

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

【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 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

【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 Solutio

145. 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]. 解题思路: 方法一:递归方法... class Solution {public: vector<int> postorderTraversal(TreeNode* root) { vector<int>

145. Binary Tree Postorder Traversal (Stack, Tree)

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? class Solution { public: vector<int> postor

145. 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]. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; *

[leedcode 145] 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]. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right

Java for LeetCode 145 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]. 后序遍历,左子树→右子树→根节点 前序遍历的非递归实现需要一个计数器,方法是需要重写一个类继承TreeNode,翁慧玉教材<数据结构:题解与拓展>P113有详细介绍,这里略.递归JAVA实现如下: public Lis