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

原创 2016年08月31日 10:02:51

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;
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

leetcode 145. Binary Tree Postorder Traversal     Total Accepted: 96378 Total Submissions: 271797...

[LeetCode]145. Binary Tree Postorder Traversal--二叉树的后序遍历

145. Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its n...

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 中序和后序恢复二叉树

1 解题思想这道题和105的思想,基本就是一致的,这里将会说的比较简单,之说下不同 Leetcode 105. Construct Binary Tree from Preorder and Ino...
  • MebiuW
  • MebiuW
  • 2016年09月16日 16:42
  • 319

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

翻译给定一个二叉树,返回其后续遍历的节点的值。例如: 给定二叉树为 {1, #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1]备注:用递归是微不足道的...
  • NoMasp
  • NoMasp
  • 2016年03月19日 22:34
  • 2953

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

原题网址:https://leetcode.com/problems/binary-tree-postorder-traversal/ Given a binary tree, return th...
  • jmspan
  • jmspan
  • 2016年05月26日 01:31
  • 266

Construct Binary Tree from Inorder and Postorder Traversal(给出中序后序求二叉树)

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume th...

[leetcode]Construct Binary Tree from Inorder and Postorder Traversal (利用中序遍历和后续遍历确定一颗二叉树)

Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal o...

Leetcode - Tree - 106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历重构二叉树)

1. Problem Description Given inorder and postorder traversal of a tree, construct the binary tree. ...

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

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

二叉树前/中/后序遍历非递归实现(用栈实现)LeetCode Binary Tree Traversal

在数据结构中,二叉树是常用的一种数据结构,而对二叉树进行遍历也是这种很常用的操作,遍历二叉树是我们可以使用递归的方法,代码很简单,但是每次调用递归函数都会都系统消耗比较大,下面介绍一下用栈的方法实现二...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode 145 Binary Tree Postorder Traversal (后序遍历二叉树)
举报原因:
原因补充:

(最多只允许输入30个字)