- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以我熟悉后序遍历:
L -> R -> P(从左到右到父级)。
我看到一段代码可以使用 2 个堆栈非常优雅地执行后序遍历:
public void postOrderTransverse(Node r){
if(r == null){return;}
Stack<Node> s = new Stack<Node>();
Stack<Node> reverser = new Stack<Node>();
s.push(r);
while(!s.isEmpty()){
Node temp = s.pop();
reverser.push(temp);
if (temp.left != null){
s.push(temp.left);}
if(temp.right != null){
s.push(temp.right);}
}
while(!reverser.empty()){
System.out.print(reverser.peek());
reverser.pop();
}
}
(通过 http://articles.leetcode.com/binary-tree-post-order-traversal/ )
本质上,一个 Stack 只是颠倒了另一个。我只是很好奇这是如何工作的。我有一个假设 Stack s 接受输入,这样输出就像 P -> R -> L,它只是将它传递给 Stack reverser,它吐出 L -> R .-> P,因为它是后进先出.
但是,只是想通过这个算法的过程来思考,我很难理解 Stack s 如何以及为什么按照它的方式接受输入。希望我能在这里有所了解!谢谢 :)
最佳答案
您提供的代码只是相应递归解决方案的循环版本:
public void postOrderTraverseRecursive(Node r){
if(r == null){return;}
if (r.left != null){
postOrderTraverseRecursive(r.left);}
if(r.right != null){
postOrderTraverseRecursive(r.right);}
System.out.print(r);
}
为了将递归转换为循环,我们需要 reverse the order of the statements execution .我们将使用一个堆栈来维护递归调用,并使用一个堆栈来维护递归的输出(即 System.out.print
语句参数)。
您的代码具有更有意义的变量名称和解释:
public void postOrderTraverse(Node root){
if(root == null){return;}
Stack<Node> recursionStack = new Stack<Node>();
Stack<Node> printStack = new Stack<Node>();
recursionStack.push(root);
while(!recursionStack.isEmpty()){
Node node = recursionStack.pop();
/* Recursion iteration start */
// According to the recursion we should process left->right->node.
// Considering that in the loop version we have to reverse the order of invocations,
// we are processing node->right->left
printStack.push(node); // node is added to printStack immediately
// left is added to recursionStack first, but because
// of the FILO nature of the stack it will be processed last
if (node.left != null){
recursionStack.push(node.left);}
// right is added to recursionStack last, but because
// of the FILO nature of the stack it will be processed first
if(node.right != null){
recursionStack.push(node.right);}
/* Recursion iteration end */
}
// We've processed all the input and now we have reversed
// history of the prints, processing them in reverse order
// to receive the actual one
while(!printStack.empty()){
System.out.print(printStack.peek());
printStack.pop();
}
}
关于java - 为什么 2 个堆叠对于后序横切如此有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39031171/
序 大家好呀,我是summo,这次来写写我在上班空闲(摸鱼)的时候做的一个小网站的事。去年阿里云不是推出了个活动嘛,2核2G的云服务器一年只要99块钱,懂行的人应该知道这个价格在业界已经是非常良心了
我尝试根据给定的级别顺序(BFS 顺序)构造 BST。我知道这是可能的,但我不知道我该怎么写。问题是我必须使用 BFS 序列。所以,我不能在这里使用递归,我必须迭代地编写我的程序......我发现这有
我是一名优秀的程序员,十分优秀!