c++ - 无需额外存储的二叉搜索树迭代前序遍历

标签 c++ algorithm tree binary-tree

对于中序二叉搜索树遍历,有一种迭代算法不使用辅助内存(堆栈、父指针、已访问标志),称为Morris Traversal。 .有没有类似的前序后序遍历算法?

最佳答案

刚想出了一个先序遍历的解决方案,可能行得通

Initialize current as root 
While current is not NULL
If current does not have right child
  a) print current root
  b) Go to the left, i.e., current = current->left
Else
  a) print current root
  a) Make the whole right sub-tree of current as the left node of the rightmost child in the left sub tree(inorder predecessor of current)
  b) Go to the left child, i.e., current = current->left

如果算法有问题请评论

关于c++ - 无需额外存储的二叉搜索树迭代前序遍历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25763218/

相关文章:

c++ - 我的 DoublyLinkedList 中的打印函数会创建重复项。 C++

javascript - JavaScript 中的简单回归预测算法

algorithm - 使用无限内存的 K&R 方法计算位数

c - KD 树 - 理解指针的困难

tree - 如何在 Racket 中折叠树形结构?

c++ - 使用每个实例颜色和偏移的 OpenGL 实例渲染

C++11: "narrowing conversion inside { }"带模数

c++ - 如果所有线程都写入不同的位置,多个线程可以同时写入文件吗?

c++ - 可以使用 pop_back 从 vector 中删除某些值吗?

编辑 Primefaces 递归树时出现 java.lang.NumberFormatException