@@ -0,0 +1,1065 @@
+package com.interview.tree;%0A%0A/**%0A * http://www.geeksforgeeks.org/construct-a-special-tree-from-given-preorder-traversal/%0A */%0Aclass PreIndex%7B%0A%09int index;%0A%7D%0Apublic class TreeFromPreOrderTraversalWith0or2Child %7B%0A%0A%09public Node createTree(int pre%5B%5D,char val%5B%5D)%7B%0A%09%09PreIndex pi = new PreIndex();%0A%09%09pi.index = 0;%0A%09%09return createTree(pre, val,pi);%0A%09%7D%0A%09%0A%09private Node createTree(int pre%5B%5D,char val%5B%5D, PreIndex ind)%7B%0A%09%09if(ind.index %3E= pre.length)%7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%09Node root = Node.newNode(pre%5Bind.index%5D);%0A%09%09%0A%09%09if(val%5Bind.index%5D == 'L')%7B%0A%09%09%09ind.index++;%0A%09%09%7Delse%7B%0A%09%09%09ind.index++;%0A%09%09%09root.left = createTree(pre,val,ind);%0A%09%09%09root.right = createTree(pre,val,ind);%0A%09%09%7D%0A%09%09return root;%0A%09%7D%0A%09%0A%09public static void main(String args%5B%5D)%7B%0A%09%09int pre%5B%5D = %7B10,20,30,40,50,60,70,80,90%7D;%0A%09%09char val%5B%5D = %7B'N','N','N','L','L','L','N','L','L'%7D;%0A%09%09TreeFromPreOrderTraversalWith0or2Child tfp = new TreeFromPreOrderTraversalWith0or2Child();%0A%09%09Node root = tfp.createTree(pre, val);%0A%09%09TreeTraversals tt = new TreeTraversals();%0A%09%09tt.preOrder(root);%0A%09%09System.out.println();%0A%09%09tt.inOrder(root);%0A%09%7D%0A%7D%0A

Preorder to binary tree