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