二进制搜索树迭代前序遍历,无需额外存储

时间:2014-09-10 10:36:03

标签: c++ algorithm tree binary-tree

对于有序二进制搜索树遍历,有一种迭代算法,它不使用称为Morris Traversal的辅助内存(堆栈,父指针,访问标志)。是否有类似的预订和后序遍历算法?

1 个答案:

答案 0 :(得分:1)

刚刚制定了预订序遍历的解决方案,可能会有效

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

如果算法出现问题,请进行评论