Pergunta de entrevista da empresa Amazon

Write a function to validate a binary tree

Respostas da entrevista

Sigiloso

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

Sigiloso

7 de out. de 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.