Amazon interview question

validate a binary tree

Interview Answers

Anonymous

6 Oct 2012

There is a difference between Binary tree and Binary search tree, so the interviewer question was for binary tree, so no need to check for the data, just check if each node in the tree has just left and right child.

1

Anonymous

4 July 2012

boolean isBST(Node root) { if (root == null) { return true; } return (isBST(root.left) && (isBST(root.right) && (root.left == null || root.left.data root.data)); }

4

Anonymous

9 July 2012

binary tree is either empty (null reference), or is made of a single node, where the left and right pointers each point to a binary tree. suppose 'BinaryTree' is the name of the class representing the binary tree implementation. public static boolean isBinaryTree(Object obj){ if(!obj instanceof BinaryTree) return false; if(null == obj || null == obj.leftChild || obj == this.rightChild ) return true; else return obj.leftChild.isBinaryTree() && obj.rightChild.isBinaryTree(); }

2