LintCode Binary Tree Inorder Traversal
Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.
Example
Given binary tree {1,#,2,3},
1 1 2 \ 3 2 4 / 5 3
return [1,3,2].
For in-order traversal we all know that firstly we want to go to the lowest level and most left part to see the node and start from that node because the smallest value of that node in the whole tree. For this problem I have two methods implemented for the question. First one is the basic one which is the recursive method as below. This is pretty straight forward as shown in the description that for each node we traverse left subtree first then visit node then traverse right subtree.
1, The traverse of the node equals traverse left subtree and then visit node, and finally traverse the right subtree.
2, The left sub-tree and right sub-tree are having less problem size than the initial problem.
3, The base case is when the node is null or children of leaves because the recursive part will visit the node before traverse right sub-tree and after traverse of left sub-tree.
1 /** 2 * Definition of TreeNode: 3 * public class TreeNode { 4 * public int val; 5 * public TreeNode left, right; 6 * public TreeNode(int val) { 7 * this.val = val; 8 * this.left = this.right = null; 9 * } 10 * } 11 */ 12 public class Solution { 13 /** 14 * @param root: The root of binary tree. 15 * @return: Inorder in ArrayList which contains node values. 16 */ 17 public ArrayList<Integer> inorderTraversal(TreeNode root) { 18 // write your code here 19 ArrayList<Integer> result = new ArrayList<Integer>(); 20 traverse(result, root); 21 return result; 22 } 23 24 public void traverse(ArrayList<Integer> list, TreeNode root) { 25 if (root == null) { 26 return ; 27 } 28 traverse(list,root.left); 29 list.add(root.val); 30 traverse(list,root.right); 31 } 32 33 }
The second way of implementation is utilizing the stack to help to iteratively traverse the nodes.
The algorithm is:
Go to the most left as deep as possilble and push all the nodes on the way to the stack
List Result
TreeNode curr;
while
curr <---- pop() first element on the top of the stack;
add to Result /print the element poped from the stack;
while curr.right exist
curr <---- curr.right
while curr is not nulld
push curr to the stack
curr <----curr.left
return (Result)
1 /** 2 * Definition of TreeNode: 3 * public class TreeNode { 4 * public int val; 5 * public TreeNode left, right; 6 * public TreeNode(int val) { 7 * this.val = val; 8 * this.left = this.right = null; 9 * } 10 * } 11 */ 12 public class Solution { 13 /** 14 * @param root: The root of binary tree. 15 * @return: Inorder in ArrayList which contains node values. 16 */ 17 public ArrayList<Integer> inorderTraversal(TreeNode root) { 18 // write your code here 19 ArrayList<Integer> result = new ArrayList<Integer>(); 20 Stack<TreeNode> stack = new Stack<TreeNode>(); 21 TreeNode curr = root; 22 23 if(curr == null) { 24 return result; 25 } 26 27 while (curr.left != null) { 28 stack.push(curr); 29 curr = curr.left; 30 } 31 32 stack.push(curr); 33 while (!stack.isEmpty()) { 34 curr= stack.pop(); 35 result.add(curr.val); 36 if (curr.right != null) { 37 curr = curr.right; 38 while (curr.left !=null ) { 39 stack.push(curr); 40 curr=curr.left; 41 } 42 stack.push(curr); 43 } 44 } 45 return result; 46 } 47 }
浙公网安备 33010602011771号