[recommend for beginners]clean C++ implementation with detailed explanation

  • 2
    class Solution {
        bool isBalanced(TreeNode* root) {
            if(!root)   return true;
            int left_depth=help(root->left);
            int right_depth=help(root->right);
            return abs(left_depth-right_depth) <= 1 && isBalanced(root->left) && isBalanced(root->right);
        int help(TreeNode* root){
            if(!root)  return 0;
            return max(help(root->left), help(root->right))+1;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.