use a stack for this { //let preorder traversal of a tree be in a array t for(i = t.length; i>-1; i--){
if(t(i) == L){ stack.push(t[i]); }else{ leftChild = s.pop(); // will return null if stack is empty rightChild = s.pop(); // will return null if stack is empty node = new Node(leftChild, rightChild); stack.push(node); } } root = stack.pop(); // get the root from the stack; } On Feb 6, 3:03 pm, algoseeker <newton.anu...@gmail.com> wrote: > I also encountered this question yesterday. This is my solution which i > tested for a few sample cases. > > https://github.com/algoseeker/Interview/blob/master/Node.java > > I maintained a pointer to the Node where there should be creation of a new > node. If the node created is left child, shift the pointer to new node > created. If this new Node's right child gets created, shift the pointer to > the parent of this Node. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.