【发布时间】:2016-07-13 21:50:42
【问题描述】:
“给出一个执行中序树遍历的非递归算法”。
这不是标准的深度优先搜索吗?我在网上看到的解决这个问题的非递归解决方案都没有做任何真正类似于 DFS 的事情(即使它们都使用堆栈)……我的推理不正确吗?
这是我的伪代码:
public inOrder(Node root):
create new Stack -> s
create new boolean set -> visited
s.push(root)
while(!s.isEmpty()):
currNode = s.pop()
print(currNode)
if(!visited.contains(currNode)):
visited.add(currNode)
if(currNode.right != null):
s.push(currNode.right)
if(currNode.left != null):
s.push(currNode.left)
【问题讨论】:
-
您可以通过选择文本并单击编辑器中的
{ }按钮来格式化您的代码。 -
我希望非递归解决方案的打印结果与递归
bst_print(head) { if (head) { bst_print(head.left); print(head.value); bst_print(head.right); }}相同。同意吗?
标签: algorithm data-structures binary-search-tree computer-science graph-algorithm