Leetcode: Binary Tree Postorder Transversal

本文介绍了一种二叉树后序遍历的方法,包括递归和迭代两种实现方式。迭代方法中提出了一种非直观但高效的实现,并通过修改先序遍历来达到目的。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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?

难度:70

recursive方法很直接:

 1 public ArrayList<Integer> postorderTraversal(TreeNode root) {
 2     ArrayList<Integer> res = new ArrayList<Integer>();
 3     helper(root, res);
 4     return res;
 5 }
 6 private void helper(TreeNode root, ArrayList<Integer> res)
 7 {
 8     if(root == null)
 9         return;
10     helper(root.left,res);
11     helper(root.right,res);
12     res.add(root.val);
13 }

Iterative 最优做法:

pre-order traversal is root-left-right.

post-order traversal is left-right-root.

We can modify pre-order traversal to be root-right-left, and traverse the tree. 

Finally, we reverse the output by the modified pre-order traversal to get post-order traversal.

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public List<Integer> postorderTraversal(TreeNode root) {
12         LinkedList<Integer> res = new LinkedList<>();
13         Stack<TreeNode> stack = new Stack<>();
14         TreeNode p = root;
15         while (p!=null || !stack.isEmpty()) {
16             if (p != null) {
17                 stack.push(p);
18                 res.addFirst(p.val);
19                 p = p.right;
20             }
21             else {
22                 TreeNode node = stack.pop();
23                 p = node.left;
24             }
25         }
26         return res;
27     }
28 }

 

 

第一次Iterative的做法就没有那么strait forward的了,需要额外用一个ArrayList<TreeNode>来记录节点的访问情况

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public List<Integer> postorderTraversal(TreeNode root) {
12         ArrayList<Integer> res = new ArrayList<Integer>();
13         if (root == null) return res;
14         ArrayList<TreeNode> visited = new ArrayList<TreeNode>();
15         LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
16         stack.push(root);
17         while (root != null || !stack.isEmpty()) {
18             if (root.left != null && !visited.contains(root.left)) {
19                 stack.push(root.left);
20                 root = root.left;
21             }
22             else if (root.right != null && !visited.contains(root.right)) {
23                 stack.push(root.right);
24                 root = root.right;
25             }
26             else {
27                 visited.add(root);
28                 res.add(stack.pop().val);
29                 root = stack.peek();
30             }
31         }
32         return res;
33     }
34 }

 

转载于:https://www.cnblogs.com/EdwardLiu/p/3972302.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值