LeetCode刷题笔录Binary Tree Post Order Traversal
来源:互联网 发布:csol刀战优化辅助 编辑:程序博客网 时间:2024/05/22 06:34
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?
Post order traversal的顺序是先访问左子树,再访问右子树,最后访问自身。这里可以用一个stack来存放待访问节点,并用一个prev指针来存储上一个访问的node,根据prev和curr的关系来决定如何前进。
每次peek一下stack的顶元素,作为curr。
情况分为:
1.prev是curr的parent
这时我们是在从上往下traverse。根据post order traversal的定义,如果curr.left != null,则将curr.left放到Stack里。如果curr.left = null,则将curr.right放进去。如果curr.left和curr.right都是null,说明curr是一个leaf node,访问之并从stack中pop出来。
2.prev是curr的left child
这时我们是在从左子树往上访问,按顺序应该将curr.right放入stack
3.prev是curr的right child
这时我们是从右子树往上访问,应该访问curr本身了。
注意只有当curr被访问时才会从stack里面将其pop出来。
public class Solution { public List<Integer> postorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<Integer>(); if(root == null) return res; TreeNode prev = null; Stack<TreeNode> s = new Stack<TreeNode>(); s.push(root); while(!s.isEmpty()){ TreeNode curr = s.peek(); //traversing down the tree if(prev == null || prev.left == curr || prev.right == curr){ if(curr.left != null){ s.push(curr.left); } else if (curr.right != null){ s.push(curr.right); } else{ //curr is a leaf node, output it res.add(curr.val); s.pop(); } } //traversing up the tree from left else if(curr.left == prev){ if(curr.right != null){ s.push(curr.right); } else{ res.add(curr.val); s.pop(); } } //traversing up the tree from right else if(curr.right == prev){ res.add(curr.val); s.pop(); } prev = curr; } return res; } }
0 0
- LeetCode刷题笔录Binary Tree Post Order Traversal
- LeetCode刷题笔录 Binary Tree Level Order Traversal
- LeetCode刷题笔录 Binary Tree Zigzag Level Order Traversal
- LeetCode刷题笔录Binary Tree Level Order Traversal II
- LeetCode刷题笔录 Binary Tree Preorder Traversal
- LeetCode刷题笔录 Binary Tree Inorder Traversal
- [leetcode刷题系列]Binary Tree Level Order Traversal II
- [leetcode刷题系列]Binary Tree Zigzag Level Order Traversal
- [leetcode刷题系列]Binary Tree Level Order Traversal
- [LeetCode] Construct/build binary tree from in-order and post-order/pre-order traversal
- LeetCode刷题笔录Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode刷题笔录Balanced Binary Tree
- LeetCode[Tree]: Binary Tree Level Order Traversal
- *(leetcode) Binary Tree Level Order Traversal (tree)
- [leetcode][tree] Binary Tree Level Order Traversal
- leetcode 每日一题 102. Binary Tree Level Order Traversal
- LeetCode: Binary Tree Level Order Traversal
- LeetCode: Binary Tree Zigzag Level Order Traversal
- 高斯消元法(Gauss Elimination) 分析 & 题解 & 模板——czyuan原创
- Android之如何做内存优化
- 007 字符串(keep it up)
- hdu 4630 No Pain No Game 树状数组
- 【java基础】Java内存机制
- LeetCode刷题笔录Binary Tree Post Order Traversal
- 细数六大支持智能手机的JS框架
- 黑马程序员-iOS学习日记(六)面向对象-核心语法(一)
- Android_利用URL实现下载功能
- 10-0. 说反话 (20)
- Eclipse导入Android项目的正确方法
- failed to lazily initialize a collection of role
- 安卓air应用热更新法
- 解决: failed to lazily initialize a collection of role: no session or session was closed问题