Leetcode - Binary Tree Postorder

[分析]
迭代实现后序遍历比迭代实现先序和中序都要稍微复杂,对其一直有恐惧心理,总觉得自己不看答案做不出来……这次竟然做出来了,很开心,练习还是有效果的~
思路1:遍历到curr节点,不管三七二十一,入栈,继续访问左节点,因为后序遍历中root最后访问。问题是栈中的节点何时出栈?答案是其左右节点均被访问之后。怎么判断其左右节点均被访问过了呢?或者换个问题,怎么判断右节点没有被访问过(一路向左,左节点必然先被访问了)?答案是如果栈顶元素有右节点且右节点没有出现在结果列表的最后。
思路2:先序遍历是root->left->right,后序遍历是left->right->root,因此修改下先序遍历实现,让其访问顺序变为root->right->left,则逆序所得结果即为后序遍历结果。


/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
// Method 2: modification of preorder from root->left->right to root->right->left
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> postorder = new ArrayList<Integer>();
if (root == null) return postorder;
LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
TreeNode curr = root;
while (curr != null || !stack.isEmpty()) {
if (curr != null) {
postorder.add(curr.val);
if (curr.left != null)
stack.push(curr.left);
curr = curr.right;
} else {
curr = stack.pop();
}
}
Collections.reverse(postorder);
return postorder;
}
// Method 1
public List<Integer> postorderTraversal1(TreeNode root) {
List<Integer> ret = new ArrayList<Integer>();
if (root == null) return ret;
LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
TreeNode curr = root;
while (curr != null || !stack.isEmpty()) {
if (curr != null) {
stack.push(curr);
curr = curr.left;
} else {
TreeNode top = stack.peek();
if (top.right != null && (ret.size() == 0 || ret.get(ret.size() - 1) != top.right.val))
curr = top.right; // right child has not been visited
else // no right child or has been visited
ret.add(stack.pop().val);
}
}
return ret;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值