Domanda di colloquio di Amazon

Write a function to validate a binary tree

Risposte di colloquio

Anonimo

7 set 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.

Anonimo

7 ott 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.