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
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,
I gues jalaj's solun works perfect.
Thanks and regards,
Gajendra Dadheech
On Sun, Feb 6, 2011 at 4:06 PM, jalaj jalaj.jaiswa...@gmail.com wrote:
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]);
My solution in more detail (in words ):-
start from the end if you get an L
make a node with data L and push its pointer in stack,
if you get a M pop two elements from stack
make them left and right children of M and then push
back m's pointer to stack(if stack is empty then stack returns NULL
but the tree can be created from a preorder walk is more than 1 right? so
the question only ask for 1?
On Feb 6, 2011 7:31 AM, jalaj jaiswal jalaj.jaiswa...@gmail.com wrote:
My solution in more detail (in words ):-
start from the end if you get an L
make a node with data L and push its pointer
no unique tree will get created i think .. as first popped is left child and
second popped is right child in algo
On Sun, Feb 6, 2011 at 10:29 PM, yq Zhang zhangyunq...@gmail.com wrote:
but the tree can be created from a preorder walk is more than 1 right? so
the question only ask for 1?
On