There are 3 solutions for solving this problem.
Java Solution 1 – Iterative
The key to solve inorder traversal of binary tree includes the following:
- The order of “inorder” is: left child -> parent -> right child
- Use a stack to track nodes
public List<Integer> inorderTraversal(TreeNode root) { ArrayList<Integer> result = new ArrayList<>(); Stack<TreeNode> stack = new Stack<>(); TreeNode p = root; while(p!=null){ stack.push(p); p=p.left; } while(!stack.isEmpty()){ TreeNode t = stack.pop(); result.add(t.val); t = t.right; while(t!=null){ stack.push(t); t = t.left; } } return result; } |
Java Solution 2 – Recursive
The recursive solution is trivial.
public class Solution { List<Integer> result = new ArrayList<Integer>(); public List<Integer> inorderTraversal(TreeNode root) { if(root !=null){ helper(root); } return result; } public void helper(TreeNode p){ if(p.left!=null) helper(p.left); result.add(p.val); if(p.right!=null) helper(p.right); } } |
Java Solution 3 – Simple
The following solution is simple, but it changes the tree structure, i.e., remove pointers to left and right children.
public List<Integer> inorderTraversal(TreeNode root) { List<Integer> result = new ArrayList<Integer>(); if(root==null) return result; Stack<TreeNode> stack = new Stack<TreeNode>(); stack.push(root); while(!stack.isEmpty()){ TreeNode top = stack.peek(); if(top.left!=null){ stack.push(top.left); top.left=null; }else{ result.add(top.val); stack.pop(); if(top.right!=null){ stack.push(top.right); } } } return result; } |
wouldn’t top.left = null modify the original tree and hence a bad style?
You could try for a full customized solution
http://www.helpwithprogramming.com/
This Solution uses an auxillary class MyTreeNode to wrap the OriginalTreeNode class in order to provide a processed tag on elements in stack. The code becomes more intuitive and required very little modification for other traversals.
import java.util.*;
//Definition for binary tree
class OriginalTreeNode {
int val;
OriginalTreeNode left;
OriginalTreeNode right;
OriginalTreeNode(int x) { val = x; }
}
//Definition for custom tree node which adds a tag
class MyTreeNode {
int val;
boolean processed;
OriginalTreeNode left;
OriginalTreeNode right;
MyTreeNode(OriginalTreeNode x)
{
val = x.val;
left = x.left;
right = x.right;
// initially set to false
processed = false;
}
}
class Solution {
public ArrayList inorderTraversal(OriginalTreeNode root) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
ArrayList lst = new ArrayList();
if(root == null)
return lst;
Stack stack = new Stack();
stack.push(new MyTreeNode(root));
while(!stack.empty()){
MyTreeNode current = stack.pop();
// if the left and right children are already added for it then just print it out.
if(current.processed){
lst.add(current.val);
continue;
}
// pushing right element as innermost on stack to visit it last
if(current.right != null)
stack.push(new MyTreeNode(current.right));
// then pushing the root and marking it as processed before doing so
current.processed = true;
stack.push(current);
// pushing left node as the outermost so it is printed first
if(current.left != null)
stack.push(new MyTreeNode(current.left));
}
return lst;
}
}
bad solutions
Another Iterative Approach:
public List inorderTraversal(TreeNode root) {
List list = new ArrayList();
if (root == null) {
return list;
}
Stack stack = new Stack();
stack.push(root);
while (stack.size() > 0) {
while (root.left != null) {
root = root.left;
stack.push(root);
}
list.add(stack.pop().val);
while (root.right == null) {
if (!stack.isEmpty()) {
root = stack.pop();
list.add(root.val);
} else {
break;
}
}
root = root.right;
if (root == null) {
break;
}
stack.push(root);
}
return list;
}
Your method type should be “Array-List”.
and you never return or print your array-list as your result.
Thank you. wish you have added Main method for running and testing, as well.
how to implement main method for this program..?
good algo, but treeContents may not work as expected as each time during recursion a new frame is created
Indeed it is. A friend just pointed it out. Apologies for the untrue comment. 🙂
Excellent !, and the Method is supposed to return TreeNode datatype value isn’t it?
public void i_inorder(treenode root)
{
Stack s=new Stack();
treenode t=root;
s.push(t);
while(!s.empty() || t!=null)
{
if(t!=null)
{
s.push(t.left);
t=t.left;
}
else
{
treenode temp=s.pop();
if(temp!=null)
{
System.out.println(temp.data);
s.push(temp.right);
t=temp.right;
}
}
}
}
Alternatively, recursion would make this much easier:
ArrayList treeContents = new ArrayList();
public inorderTraversal(TreeNode node){
if (node != null){
inorderTraversal(node.left);
treeContents.add(node.val);
inorderTraversal(node.right);
}
else {return null;}
}
No, it is in order traversal and is very elegant
This algorithm is no where near an inorder traversal