关闭

LeetCode 145 Binary Tree Postorder Traversal (后序遍历二叉树)

标签: leetcode后序遍历二叉树
676人阅读 评论(0) 收藏 举报
分类:

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?

题目链接:https://leetcode.com/problems/binary-tree-postorder-traversal/

递归:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    
    public void DFS(TreeNode root, List<Integer> ans) {
        if(root == null) {
            return;
        }
        DFS(root.left, ans);
        DFS(root.right, ans);
        ans.add(root.val);
    }
    
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        DFS(root, ans);
        return ans;
    }
}

非递归:后序遍历左右中,考虑到栈的先进先出,故先将右子树压栈,两种情况记录答案出栈,1是遇到叶子,2是父节点的子节点已经被访问过,根据压栈顺序,可以保证这样可以完成后序遍历

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        Stack<TreeNode> stk = new Stack<>();
        if(root != null) {
            stk.push(root);
        }
        TreeNode cur = null;
        TreeNode pre = null;
        while(!stk.empty()) {
            cur = stk.peek();
            if(cur.left == null && cur.right == null) {
                ans.add(cur.val);
                pre = cur;
                stk.pop();
            }
            else if(pre != null && (cur.left == pre || cur.right == pre)) {
                ans.add(cur.val);
                pre = cur;
                stk.pop();
            }
            else {
                if(cur.right != null) {
                    stk.push(cur.right);
                }
                if(cur.left != null) {
                    stk.push(cur.left);
                }
            }
        }
        return ans;
    }
}


0
0
查看评论

LeetCode(145)Binary Tree Postorder Traversal

题目如下: Given a binary tree, return the postorder traversal of its nodes' values. 1 \ 2 / 3 return [3,2,1]. Note: Recursive so...
  • feliciafay
  • feliciafay
  • 2014-01-16 04:01
  • 3574

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

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/problems/binary-tree-postorder-traversal/ Hard 级...
  • crazy1235
  • crazy1235
  • 2016-05-25 00:48
  • 5206

LeetCode145 Binary Tree Postorder Traversal Java题解(递归 迭代)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, ...
  • u012249528
  • u012249528
  • 2015-07-09 10:34
  • 904

Binary Tree Postorder Traversal -- LeetCode

原题链接: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/  跟Binary Tree Inorder Traversal以及Binary Tree Preorder Traversal一样,二...
  • linhuanmars
  • linhuanmars
  • 2014-03-25 01:10
  • 12913

[LeetCode][Java] 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...
  • Evan123mg
  • Evan123mg
  • 2015-07-20 21:23
  • 646

106 Construct Binary Tree from Inorder and Postorder Traversal

题目链接:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/题目:Given inorder and postorder traversal of a tree, cons...
  • ChilseaSai
  • ChilseaSai
  • 2015-12-09 20:29
  • 875

LeetCode Given a binary tree, return the postorder traversal of its nodes' values.

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree{1,#,2,3}, 1 \ ...
  • yyywww666
  • yyywww666
  • 2017-04-28 11:29
  • 522

【LeetCode-面试算法经典-Java实现】【106-Construct Binary Tree from Inorder and Postorder Traversal(构造二叉树II)】

【106-Construct Binary Tree from Inorder and Postorder Traversal(通过中序和后序遍历构造二叉树II)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given inorder and postorder tr...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-09 06:39
  • 2212

LeetCode Construct Binary Tree from Inorder and Postorder Traversal 思考分析过程分享

思考: 1 画图: 把二叉树和其中序和后序的序列都画出来(这个很重要,我以前有时候总是凭空想象,结果脑子一片空白) 2 把问题写下来:怎么构造一个节点? 3 观察: 到底有什么特点是可以利用来解题的呢? 感觉好辛苦,一个编程任务,如临大敌似的!不知道是不是功力不到。 突然想到这是不就像是金庸武...
  • kenden23
  • kenden23
  • 2013-11-12 13:20
  • 2290

LeetCode 145:Binary Tree Postorder Traversal(后序遍历)

Given a binary tree, return the postorder traversal of its nodes' values.
  • sunao2002002
  • sunao2002002
  • 2015-05-23 01:38
  • 979
    个人资料
    • 访问:744435次
    • 积分:15343
    • 等级:
    • 排名:第875名
    • 原创:818篇
    • 转载:7篇
    • 译文:0篇
    • 评论:123条
    博客专栏