[LeetCode] Nonrecursive postorder traversal 非递归后续遍历
来源:互联网 发布:2016windows系统的手机 编辑:程序博客网 时间:2024/05/22 04:35
简单的做法是,用一个栈来保存遍历时的节点,用另外一个栈保存经过节点的次数。下面的算法不需要节点次数的栈。
翻译自:http://leetcode.com/2010/10/binary-tree-post-order-traversal.html
We use a prev variable to keep track of the previously-traversed node. Let’s assume curr is the current node that’s on top of the stack. When prev is curr‘s parent, we are traversing down the tree. In this case, we try to traverse to curr‘s left child if available (ie, push left child to the stack). If it is not available, we look at curr‘s right child. If both left and right child do not exist (ie, curr is a leaf node), we print curr‘s value and pop it off the stack.
If prev is curr‘s left child, we are traversing up the tree from the left. We look at curr‘s right child. If it is available, then traverse down the right child (ie, push right child to the stack), otherwise print curr‘s value and pop it off the stack.
If prev is curr‘s right child, we are traversing up the tree from the right. In this case, we print curr‘s value and pop it off the stack.
The above method is easy to follow, but has some redundant code. We could refactor out the redundant code, and now it appears to be more concise. Note how the code section for printing curr‘s value get refactored into one single else block. Don’t worry about in an iteration where its value won’t get printed, as it is guaranteed to enter the else section in the next iteration.
Alternative Solution:
An alternative solution is to use two stacks. Try to work it out on a piece of paper. I think it is quite magical and beautiful. You will think that it works magically, but in fact it is doing a reversed pre-order traversal. That is, the order of traversal is a node, then its right child followed by its left child. This yields post-order traversal in reversed order. Using a second stack, we could reverse it back to the correct order.
Here is how it works:
- Push the root node to the first stack.
- Pop a node from the first stack, and push it to the second stack.
- Then push its left child followed by its right child to the first stack.
- Repeat step 2) and 3) until the first stack is empty.
- Once done, the second stack would have all the nodes ready to be traversed in post-order. Pop off the nodes from the second stack one by one and you’re done.
Complexity Analysis:
The two-stack solution takes up more space compared to the first solution using one stack. In fact, the first solution has a space complexity of O(h), where h is the maximum height of the tree. The two-stack solution however, has a space complexity of O(n), where n is the total number of nodes.
- [LeetCode] Nonrecursive postorder traversal 非递归后续遍历
- leetcode 145. Binary Tree Postorder Traversal-后续遍历|递归|非递归
- LeetCode Binary Tree Postorder Traversal 二叉树后序遍历 递归和非递归解法
- 【LeetCode】Binary Tree Postorder Traversal 二叉树后序遍历递归以及非递归算法
- Binary Tree Postorder Traversal 二叉树后续遍历@LeetCode
- leetcode 145. Binary Tree Postorder Traversal 二叉树后续遍历
- leetcode---binary-tree-postorder-traversal---树后续遍历
- LeetCode Binary Tree Postorder Traversal(二叉树的后序遍历 非递归实现)
- ****(leetcode) Binary Tree Postorder Traversal (tree后序遍历非递归实现)
- 【leetcode】145Binary Tree Postorder Traversal(二叉树非递归后序遍历)
- [LeetCode] Nonrecursive preorder traversal
- 【Leetcode】145. Binary Tree Postorder Traversal 【递归&&非递归】
- Binary Tree Postorder Traversal 非递归实现二叉树后序遍历
- 非递归后续遍历
- [leetcode]Binary Tree Postorder Traversal 二叉树后序非递归遍历
- 【LeetCode-面试算法经典-Java实现】【145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)】
- LeetCode 145. Binary Tree Postorder Traversal (非递归和递归解)
- 【二叉树的后续遍历】Binary Tree Postorder Traversal
- SQL Server关闭数据库连接
- QGraphicsView/Scene框架
- 北大oj 1423- Big Number
- 给程序员的忠告
- REST 中如何安全地处理用户登录问题?
- [LeetCode] Nonrecursive postorder traversal 非递归后续遍历
- What is the difference between max-device-width and max-width for mobile web?
- python各种类型转换-int,str,char,float,ord,hex,oct等
- struts2 表单对象没有自动实例化
- failed to open stream: Permission denied in
- Copy List with Random Pointer
- 排名(杭电oj1236)(简单排序)
- What Is A CSS Reset?
- 026Java文件数据IO操作