Preorder binary tree traversal is a classic interview problem. The key to solve this problem is using a stack to store left and right children, and push right child first so that it is processed after the left child.
Java Solution
public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public class Solution { public ArrayList<Integer> preorderTraversal(TreeNode root) { ArrayList<Integer> returnList = new ArrayList<Integer>(); if(root == null) return returnList; Stack<TreeNode> stack = new Stack<TreeNode>(); stack.push(root); while(!stack.empty()){ TreeNode n = stack.pop(); returnList.add(n.val); if(n.right != null){ stack.push(n.right); } if(n.left != null){ stack.push(n.left); } } return returnList; } } |
@Prabhat, I understood your solution but the input seems hardcoded. How can we enter user-input for nodes with variable length of tree and display pre, post and inorder traversals.
public class Solution{
public void preOrder(TreeNode root){
if(root==null)return;
System.out.println(root.val);
preOrder(root.left);
preOrder(root.right);
}
}
// Recursive version based on previous comments
class Solution {
private void preorderTraversalRec (TreeNode root, List orderedList) {
if (root == null)
return;
orderedList.add(root.val);
preorderTraversalRec(root.left, orderedList);
preorderTraversalRec(root.right, orderedList);
}
public List preorderTraversal(TreeNode root) {
List orderedList = new ArrayList();
if (root == null) return orderedList;
preorderTraversalRec(root, orderedList);
return orderedList;
}
}
OFC but If you read the problem on leetCode, you would see that they asked to try an iterative solution!
http://www.javabeat.net/binary-search-tree-traversal-java/
//Recursive Preorder, Inorder, PostOrder traversals.
import java.util.ArrayList;
import java.util.List;
public class TreeTraversal {
static List orderedList = new ArrayList();
public static void preOrderTraversal(TreeNode root){
if(root == null)
return;
orderedList.add(root.val);
preOrderTraversal(root.left);
preOrderTraversal(root.right);
}
public static void inOrderTraversal(TreeNode root){
if(root == null)
return;
inOrderTraversal(root.left);
orderedList.add(root.val);
inOrderTraversal(root.right);
}
public static void postOrderTraversal(TreeNode root){
if(root == null)
return;
postOrderTraversal(root.left);
postOrderTraversal(root.right);
orderedList.add(root.val);
}
public static void main(String[] args) {
TreeNode root = new TreeNode(2);
TreeNode left = new TreeNode(1);
TreeNode right = new TreeNode(3);
root.left = left;
root.right = right;
System.out.print(“nPreorder traversal of 2 1 3: “);
preOrderTraversal(root);
for(Integer i:orderedList)
System.out.print(i + ” “);
orderedList.clear();
System.out.print(“nIneorder traversal of 2 1 3: “);
inOrderTraversal(root);
for(Integer i:orderedList)
System.out.print(i + ” “);
orderedList.clear();
System.out.print(“nPostorder traversal of 2 1 3: “);
postOrderTraversal(root);
for(Integer i:orderedList)
System.out.print(i + ” “);
}
}
/*
OUTPUT:
Preorder traversal of 2 1 3: 2 1 3
Ineorder traversal of 2 1 3: 1 2 3
Postorder traversal of 2 1 3: 1 3 2
*/
it would be better to use recursion rather than imperative to solve binary tree traversal.
here is my version.
private List traversePreOrder(TreeNode root) {
if(root == null) return Collections.EMPTY_LIST;
List ordered = new ArrayList();
ordered.add(root.value);
ordered.addAll(traversePreOrder(root.left));
ordered.addAll(traversePreOrder(root.right));
return ordered;
}