use flag to check~


  • 0
    K

    class Solution {
    public:
    bool isBalanced(TreeNode* root) {
    deep(root,0);
    if(flag)return false;
    return true;
    }
    int flag=0;
    int deep(TreeNode* root,int deepth){
    if(root==NULL)return deepth;
    int left=deepth,right=deepth;
    left=deep(root->left,deepth+1);
    right=deep(root->right,deepth+1);
    if(abs(left-right)>1)flag=1;
    return max(left,right);
    }
    };


Log in to reply
 

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