Given a binary search tree, print the elements in-order iteratively without using recursion.

Note:
Before you attempt this problem, you might want to try coding a pre-order traversal iterative solution first, because it is easier. On the other hand, coding a post-order iterative version is a challenge. See my post: Binary Tree Post-Order Traversal Iterative Solution for more details and an in-depth analysis of the problem.

We know the elements can be printed in-order easily using recursion, as follow:

 
1
2
3
4
5
6
voidin_order_traversal(BinaryTree *p){
  if(!p)return;
  in_order_traversal(p->left);
  cout<<p->data;
  in_order_traversal(p->right);
}

Excessive recursive function calls may cause memory to run out of stack space and extra overhead. Since the depth of a balanced binary search tree is about lg(n), you might not worry about running out of stack space, even when you have a million of elements. But what if the tree is not balanced? Then you are asking for trouble, because in the worst case the height of the tree may go up to n. If that is the case, stack space will eventually run out and your program will crash.

To solve this issue, we need to develop an iterative solution. The idea is easy, we need a stack to store previous nodes, and a visited flag for each node is needed to record if the node has been visited before. When a node is traversed for the second time, its value will be printed. After its value is printed, we push its right child and continue from there.

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
voidin_order_traversal_iterative(BinaryTree *root){
  stack<BinaryTree*>s;
  s.push(root);
  while(!s.empty()){
    BinaryTree *top=s.top();
    if(top!=NULL){
      if(!top->visited){
        s.push(top->left);
      }else{
        cout<<top->data<<" ";
        s.pop();
        s.push(top->right);
      }
    }else{
      s.pop();
      if(!s.empty())
        s.top()->visited=true;
    }
  }
}

Alternative Solution:
The above solution requires modification to the original BST data structure (ie, adding a visited flag). The other solution which doesn’t modify the original structure is with the help of a current pointer in addition of a stack.

First, the current pointer is initialized to the root. Keep traversing to its left child while pushing visited nodes onto the stack. When you reach a NULL node (ie, you’ve reached a leaf node), you would pop off an element from the stack and set it to current. Now is the time to print current’s value. Then, current is set to its right child and repeat the process again. When the stack is empty, this means you’re done printing.

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
voidin_order_traversal_iterative(BinaryTree *root){
  stack<BinaryTree*>s;
  BinaryTree *current=root;
  booldone=false;
  while(!done){
    if(current){
      s.push(current);
      current=current->left;
    }else{
      if(s.empty()){
        done=true;
      }else{
        current=s.top();
        s.pop();
        cout<<current->data<<" ";
        current=current->right;
      }
    }
  }
}

We can even do better by refactoring the above code. The refactoring relies on one important observation:

The last traversed node must not have a right child.

Why this is true? To prove this, we assume the opposite, that is: the last traversed node has a right child. This is certainly incorrect, as in-order traversal would have to traverse its right child next before the traversal is done. Since this is incorrect, the last traversed node must not have a right child by contradiction.

Below is the refactored code:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
voidin_order_traversal_iterative(BinaryTree *root){
  stack<BinaryTree*>s;
  BinaryTree *current=root;
  while(!s.empty()||current){
    if(current){
      s.push(current);
      current=current->left;
    }else{
      current=s.top();
      s.pop();
      cout<<current->data<<" ";
      current=current->right;
    }
  }
}

threaded tree, with the special threading links shown by dashed arrows. A threaded binary tree makes it possible to traverse the values in the binary tree via a linear traversal that is more rapid than a recursive in-order traversal.

Further Thoughts:
The above solutions require the help of a stack to do in-order traversal. Is it possible to do in-order traversal without a stack?

The answer is yes, it’s possible. There’s 2 possible ways that I know of:

    1. By adding a parent pointer to the data structure, this allows us to return to a node’s parent (Credits to my friend who provided this solution to me). To determine when to print a node’s value, we would have to determine when it’s returned from. If it’s returned from its left child, then you would print its value then traverse to its right child, on the other hand if it’s returned from its right child, you would traverse up one level to its parent.
    2. By using a Threaded Binary Tree. Read the article: Threaded Binary Tree on Wikipedia for more information.
 public class Solution {
public ArrayList<Integer> inorderTraversal(TreeNode root) {
Stack<TreeNode> st = new Stack<TreeNode>();
ArrayList<Integer> result = new ArrayList<Integer>();
if(root == null) return result;
boolean fin = false;
while(!fin){
if(root != null){
st.push(root);
root = root.left;
}else{
if(st.size() == 0){
fin = true;
}else{
root = st.pop();
result.add(root.val);
root = root.right;
}
}
}
return result;
}
}

这个代码是错误的:

 public List<Integer> inorderTraversal(TreeNode root) {
// write your code here
LinkedList<TreeNode> stack = new LinkedList<TreeNode> (); //stack
List<Integer> result = new ArrayList<Integer> ();
if(root == null) return result;
stack.push(root);
while(!stack.isEmpty()){
TreeNode tmp = stack.peek();
if(tmp.left != null) stack.push(tmp.left);
else{
tmp = stack.pop();
result.add(tmp.val);
if(tmp.right != null) stack.push(tmp.right);
}
}
return result;
}

会在最后一个root 和其left leaf之间无限循环。

Binary Search Tree In-Order Traversal Iterative Solution的更多相关文章

  1. [Leetcode][JAVA] Recover Binary Search Tree (Morris Inorder Traversal)

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  2. [Swift]LeetCode1008. 先序遍历构造二叉树 | Construct Binary Search Tree from Preorder Traversal

    Return the root node of a binary search tree that matches the given preorder traversal. (Recall that ...

  3. LeetCode 1008. Construct Binary Search Tree from Preorder Traversal

    原题链接在这里:https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/ 题目: Retu ...

  4. 【leetcode】1008. Construct Binary Search Tree from Preorder Traversal

    题目如下: Return the root node of a binary search tree that matches the given preorder traversal. (Recal ...

  5. 【LeetCode】 99. Recover Binary Search Tree [Hard] [Morris Traversal] [Tree]

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  6. 【LeetCode】1008. Construct Binary Search Tree from Preorder Traversal 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  7. leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)

    https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary searc ...

  8. 算法与数据结构基础 - 二叉查找树(Binary Search Tree)

    二叉查找树基础 二叉查找树(BST)满足这样的性质,或是一颗空树:或左子树节点值小于根节点值.右子树节点值大于根节点值,左右子树也分别满足这个性质. 利用这个性质,可以迭代(iterative)或递归 ...

  9. LeetCode解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal

    1. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that ...

随机推荐

  1. Codevs 1039 :数的划分

    总时间限制: 1000ms 内存限制:  65536kB 描述 将整数n分成k份,且每份不能为空,任意两份不能相同(不考虑顺序). 例如:n=7,k=3,下面三种分法被认为是相同的. 1,1,5: 1 ...

  2. upTodown

         ------->>>  从左图变为有图,并实现将左图上面的信息隐藏. <img src="../images/up.gif" border=&qu ...

  3. berkerly db 中简单的读写操作(有一些C的 还有一些C++的)

    最近在倒腾BDB,才发现自己确实在C++这一块能力很弱,看了一天的api文档,总算是把BDB的一些api之间的关系理清了,希望初学者要理清数据库基本知识中的环境,句柄,游标的基本概念,这样有助于你更好 ...

  4. Linux 内存布局

         本文主要简介在X86体系结构下和在ARM体系结构下,Linux内存布局的概况,力求简单明了,不过多深入概念,多以图示的方式来记忆理解,一图胜万言. Technorati 标签: 内存 布局 ...

  5. 使用struct实现面向对象编程的封装

    虽然C是面向过程的语言,但是这不代表C不能使用面向对象的思想,本质上说语言只是一种手段而已,一种外在的表现形式,支持面向对象的语言只是通过设计的特定的关键字更好的表现了面向对象编程而已.C中也可以使用 ...

  6. 《gpg文件加密的使用》RHEL6

    甲端: 首先是要生成一对密钥: 提示是否要生成2048个字节的密钥对:   下面都是生成密钥对时的步骤: 按“o”键开始生成密钥对: 提示要我给密钥对加个密码: 输入2次 之后密钥对的字符需要我按键盘 ...

  7. 《linux下sudo服务的使用》RHEL6

    /bin/ 下放的二进制文件命令都是普通用户可以使用的 Sbin 下放的二进制文件命令都是超级用户root可以使用的   普通用户也想使用Sbin下的文件可以通过sudo来实现: 默认普通用户是不可以 ...

  8. Matlab2012a第一次安装打不开 查找程序安装类时出错

    打开bin文件夹下的matlab!!!!!!进行激活~

  9. Bootstrap轮播(carousel)插件中图片变形的终极解决方案——使用jqthumb.js

    在顶求网的首页中我使用了BootStrap的轮播(carousel)插件来展示文章中的图片.我在程序中自动抓取文章的第一张图片作为该轮播控件中要显示的图片,由于文章的图片大小不一,而轮播插件的大小基本 ...

  10. 【全面解析DeepZoom 之三】建立DeepZoom应用

    文章出处:http://www.cnblogs.com/zhouyinhui/archive/2008/04/14/1153371.html (周银辉) 与导出整图不一样,你不能这样使用: <M ...