Guest User

Untitled

a guest
Dec 16th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.36 KB | None | 0 0
  1. int maxDepth(TreeNode *root) {
  2. if(!root) return 0;
  3. return max(maxDepth(root->left), maxDepth(root->right)) + 1;
  4. }
  5.  
  6. bool isBalanced(TreeNode* root) {
  7. if(!root) return true;
  8. int leftHeight = maxDepth(root->left);
  9. int rightHeight = maxDepth(root->right);
  10. return abs(leftHeight - rightHeight) <= 1 && isBalanced(root->left) && isBalanced(root->right);
  11. }
Add Comment
Please, Sign In to add comment