Lunatic Server Solutions Interview Question for Developer Program Engineers
- 0of 0 votes
In this problem you have to write a program to check if two given binary trees are structurally equivalent.- PriyaDarad May 22, 2012 in United States
Two trees are structurally equivalent if they are both null or if the left and right children of one are
structurally equivalent to the RESPECTIVE children of the other. In other words, when you draw the trees
on paper, they should LOOK alike (ignoring the values at the nodes).
The input to the program is a number N followed by N lines of input. Each line consists of a sequence of
positive numbers terminated by -1. There will be no duplicate numbers in any of the lines.
Construct a binary search tree with the input in the second line and use this as the basis-tree. For each of the
remaining N-1 lines, construct a binary search tree and compare against the basis tree for equivalence. If the
trees are equivalent, print YES else print NO. Also print the depth difference between the two trees (ie, depth
of the bigger tree minus the depth of the smaller tree). Both these for a given tree pair must be on one line
separated by a space. The answers for the different pairs must be on separate lines.
1 3 2 4 -1
4 1 2 3 -1
3 2 1 4 -1
4 3 2 1 -1
1 3 4 2 -1
(Note that the depth difference will be zero if the trees are equivalent.)
| Report Duplicate | Flag | PURGE
Lunatic Server Solutions Developer Program Engineer
Interview Type: Written Test
Open Chat in New Window