Amazon interview question

Write a function to validate a binary tree

Interview Answers

Anonymous

7 Sept 2011

A tree is invalid if a node is pointed to more than once (e.g., both the left and right child point to the same node). So, traverse the tree and check for any repeats. You can check for repeats by either marking every node as you traverse it or hashing the node in a hash table.

Anonymous

7 Oct 2011

In addition to the above, if you want to validate that the tree is a binary search tree, as opposed to just a binary tree, you would perform an inorder traversal and make sure that the keys are always increasing.

Glassdoor | Glassdoor