Amazon Interview Question

the second question was a little tougher, "Write a function that checks whether a binary tree is valid or not. A valid binary tree is a tree where no child node points to any of its ancestors"

Interview Answers

Anonymous

Nov 1, 2011

Use BFS with a queue, for each new element q[n] n is even check if q[n/2] is pointing to the same element.

Anonymous

Nov 1, 2011

On second thought, DFS should be used. This is to imitate a linked list loop detection situation. So we have to make sure that every time we check we have to make sure that all the elements should be in the same line.