Question d’entretien chez Amazon

validate a binary tree

Réponses aux questions d'entretien

Utilisateur anonyme

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

Utilisateur anonyme

4 juill. 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

Utilisateur anonyme

9 juill. 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