Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class solution {
- public:
- int dfsHeight (TreeNode *root) {
- if (root == NULL) return 0;
- int leftHeight = dfsHeight (root -> left);
- if (leftHeight == -1) return -1;
- int rightHeight = dfsHeight (root -> right);
- if (rightHeight == -1) return -1;
- if (abs(leftHeight - rightHeight) > 1) return -1;
- return max (leftHeight, rightHeight) + 1;
- }
- bool isBalanced(TreeNode *root) {
- return dfsHeight (root) != -1;
- }
- };
Add Comment
Please, Sign In to add comment