Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1
    \
     2
    /
   3

 

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.


OJ's Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

Here's an example:

   1
  / \
 2   3
    /
   4
    \
     5
The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
 
 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     ArrayList<Integer> result = null;
12     public ArrayList<Integer> inorderTraversal(TreeNode root) {
13         // IMPORTANT: Please reset any member data you declared, as
14         // the same Solution instance will be reused for each test case.
15         result = new ArrayList<Integer>();
16         inorder(root);
17         return result;
18     }
19     public void inorder(TreeNode root){
20         if(root == null) return;
21         inorder(root.left);
22         result.add(root.val);
23         inorder(root.right);
24     }
25 }

 

 1 //1.build threaded binary tree
 2 public List<Integer> inorderTraversal(TreeNode root) {
 3     // write your code here
 4     TreeNode cur = null, pre = null;
 5     List<Integer> result = new ArrayList<Integer>();
 6     if(root == null) return result;
 7     cur = root;
 8     while(cur != null){
 9         if(cur.left == null){
10             result.add(cur.val);
11             cur = cur.right;
12         }else{
13             pre = cur.left;
14             while(pre.right != null && pre.right != cur){
15                 pre = pre.right;
16             }
17             if(pre.right == null){//first time: build threaded binary tree
18                 pre.right = cur;
19                 cur = cur.left;
20             }else{//second time: recover tree and output val
21                 result.add(cur.val);
22                 pre.right = null;
23                 cur = cur.right;
24             }
25             
26         }
27     }
28     return result;
29 }

 

 1 //2. stack
 2 public List<Integer> inorderTraversal(TreeNode root) {
 3     List<Integer> result = new ArrayList<Integer>();
 4     if(root == null) return result;
 5     LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
 6     boolean flg = true;
 7     while(flg){
 8         if(root != null){
 9             stack.push(root);
10             root = root.left;
11         }else{
12             if(stack.isEmpty()) flg = false;
13             else{
14                 root = stack.pop();
15                 result.add(root.val);
16                 root = root.right;
17             }
18         }
19     }
20     return result;
21 }

 

posted on 2013-11-15 13:16  Step-BY-Step  阅读(167)  评论(0编辑  收藏  举报

导航