LeetCode刷题笔录Binary Tree Post Order Traversal
2014-08-18 01:06
218 查看
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree
return
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出来。
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; } }
相关文章推荐
- Leetcode-Binary Tree Postorder Traversal
- LeetCode-Binary Tree Postorder Traversal
- leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f
- LeetCode - Binary Tree Postorder Traversal
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]Binary Tree Postorder Traversal
- LeetCode——Construct Binary Tree from Inorder and Postorder Traversal
- leetcode Binary Tree Postorder Traversal
- [Leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
- LeetCode "Construct Binary Tree from Inorder and Postorder Traversal"
- [LeetCode] Binary Tree Postorder Traversal
- 【LeetCode】Construct Binary Tree from Inorder and Postorder Traversal
- leetcode--Binary Tree Postorder Traversal
- LeetCode145—Binary Tree Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 中后序构造BST
- LeetCode (Binary Tree Postorder Traversal)
- LeetCode145. Binary Tree Postorder Traversal(hard)
- Leetcode: Binary Tree Postorder Traversal