Bloomberg Interview Question

Find if a binart search tree is Balanced

Interview Answer

Anonymous

Mar 2, 2014

int height(treeNodePtr root) { return (!root) ? 0: 1 + MAX(height(root->left),height(root->right)); } bool isHeightBalanced(treeNodePtr root) { return (root == NULL) || (isHeightBalanced(root->left) && isHeightBalanced(root->right) && abs(height(root->left) - height(root->right)) <=1); }