遍历二叉搜索树
我正在阅读 算法简介 我遇到了这个关于 In-order 的问题不使用堆栈或递归的二叉搜索树的遍历。提示说假设测试指针是否相等是一个合法的操作。我一直在寻找这个问题的解决方案。请给我一些指导。我不是在寻找代码。只要给我正确的方向。
完全重复此处
I was reading through Introduction to algorithms i came across this problem about In-order Traversal of binary search tree without using a stack or recursion. Hint says to assume that testing of pointers for equality is a legitimate operation.I'm stuck finding the solution to this problem. Please give me some direction. I'm not looking for the code. Just give me the right direction.
Exact duplicate here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有堆栈或递归意味着您必须使用指针。没有给你代码,也没有给你确切的答案,因为你要求不要:)
想想如何在不使用递归的情况下探索树:你需要做什么?您需要保留什么指针?树节点可以有指向父节点的指针吗?
希望有帮助。
No stack nor recursion means you have to use pointers. Not giving you code nor the exact answer, since you asked not to :)
Think about how to explore the tree without using recursion: what would you need to do? What pointer(s) do you need to keep? Can a tree node have a pointer to the parent?
Hope it helps.
我们需要一个线程二叉树来进行无递归/堆栈的有序遍历。
维基百科说“二叉树是通过使所有通常为空的右子指针指向该节点的中序后继者,以及所有通常为空的左子指针指向该节点的中序前驱者来线程化的”
所以你是给定一个普通的二叉树,将其转换为线程二叉树,这可以使用莫里斯遍历来完成。
在莫里斯遍历中,您要做的是将每个节点与其有序后继节点连接起来。所以在访问一个节点时,搜索它的有序前驱节点并设其为Pred。
然后使 Pred->right=Current 节点,我们也必须恢复更改。您可以更好地参考此 http://www.geeksforgeeks.org/archives/6358 以获得精彩的内容解释。
We need a Threaded Binary Tree to do in-order Traversal without recursion / stack.
Wiki says 'A binary tree is threaded by making all right child pointers that would normally be null point to the inorder successor of the node, and all left child pointers that would normally be null point to the inorder predecessor of the node'
So you are given a normal Binary Tree , Convert it into a Threaded Binary Tree which can be done using Morris Traversal.
What you are going to do in Morris Traversal is to connect each node with its in-order successor. So while visiting a node ,Search for its in-order predecessor and let it be Pred.
then make Pred->right=Current node and we have to revert back the changes too. You can better refer this http://www.geeksforgeeks.org/archives/6358 for a great explanation.
你好Parminder我已经在java中实现了你的问题。请检查一次
对于TreeNode类,下面的代码将帮助你..
Hello Parminder i have implemented your question in java.Please check it once
For TreeNode class below code will help you..