## Yahoo Interview Question for Software Engineer / Developers

Comment hidden because of low score. Click to expand.
0
of 0 vote

{
return true;
return false;
return false;
}

Comment hidden because of low score. Click to expand.
0

doesn't your 2 inorder loops scan the whole tree twice? remember inorder traverses the whole tree, not half tree?

Comment hidden because of low score. Click to expand.
0

NO ! If you observe the code carefully there are no loops ! :P
Solution is perfect.

Comment hidden because of low score. Click to expand.
0

Remember, the question does not say whether it is a BT or BST. So this could be any kind of tree with n-children

Comment hidden because of low score. Click to expand.
0

Traverse one tree and keep a hash map, for each node add a <key,pair<,>> pair where key is the parent node info and pair are the two children (duplicates can be resolved via chaining). Now traverse next tree and see if same node (value) has same children as found in previous tree based on the hash table values.

Comment hidden because of low score. Click to expand.
0
of 0 vote

As significant optimization:

``````if( tree1.size() != tree2.size() ){
return false;
}

// TODO: make in-order (or any other) traversation and compare``````

Comment hidden because of low score. Click to expand.
0

computing the size of tree requires O(n) where n is the number of nodes in tree.

Comment hidden because of low score. Click to expand.
0
of 0 vote

breadth first traversal on each tree ,each time comparing the values.

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````bool isSame(struct node* head1, struct node* head2)
{
else return true;
}``````

Comment hidden because of low score. Click to expand.
0

nice answer, if we can rewrite like this

``````bool isSame(struct node* head1, struct node* head2)
{

return true;
else
return false;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````#define LEFT(n) (n->left == NULL) ? 0 : 1
#define RIGHT(n) (n->right == NULL) ? 0 : 1

bool sameTree(btree *p, btree *q)
{
if(p == NULL && q == NULL)
return true;

if(p->key != q->key)
return false;

if(LEFT(p) != LEFT(q))
return false;

if(RIGHT(p) != RIGHT(q))
return false;

return sameTree(p->left, q->left) && sameTree(p->right, q->right);
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

Well this is just a simple recursion.

``````int check(struct node *root1, struct node *root2) {
if(root1 == NULL && root2 == NULL) {
return 1;
}
else if(root1 && root2) {
return(root1->data == root2->data && check(root1->left,root2->left) && check(root1->right,root2->right));
}
else {
return 0;
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

If it BST, Do an In order traversal for both the trees, it will be in sorted sequence, then compare the values from left to right

Comment hidden because of low score. Click to expand.
0
of 0 vote

Traverse both trees recursively together. This will even check if they have the same structures.

isEqual(root1, root2);

boolean isEqual(Node node1, Node node2){
if(node1.data == node2.data){
return isEqual(node1.left, node2.left)
&& isEqual(node1.right, node2.right);
}
return false;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

Traverse both trees recursively together. This will even check if they have the same structures.

isEqual(root1, root2);

boolean isEqual(Node node1, Node node2){
if(node1==null && node2 == null){
return true;
}else if(node1==null){// this means only node2 is null
return false;
}
if(node1.data == node2.data){
return isEqual(node1.left, node2.left)
&& isEqual(node1.right, node2.right);
}
return false;
}

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

### Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

### Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.