Interview Question






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

As per question, since there is no constraint on the nodes of the merged tree, I will make the root of second tree as child of first tree's leaf node. i.e. leafNode-> secondTree'sRoot.
Recall that order of reaching tree's leaf node from the root is O(n/2).

- Anonymous June 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Remove each node from the one tree in preorder fashion and add it to the other tree.

- Anonymous June 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Two trees? Not BST?
Try this:?

root = new Node(newData, root1, root2);

- Anonymous June 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if in case of merging two BST....
take out nodes one by one in in-order...from both the trees
then compare the 2 node values...
then construct a new tree based on those values........

- vigneshk.cit June 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

NA

- weijiang2009 February 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

create a sorted list of each tree
then merge the two sorted list
create a tree out of new list
O(n1+n2).

- Anonymous August 14, 2011 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

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.

Learn More

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.

Learn More