C++ Top down DFS 9ms


  • 0
     int DFS(TreeNode* root){
            if(root==NULL) return 0;
            int left=DFS(root->left)+1;
            int right=DFS(root->right)+1;
            return max(left,right);
        }
        
        bool isBalanced(TreeNode* root) {
            if(root==NULL) return true;
            int left=DFS(root->left);
            int right=DFS(root->right);
            return abs(left-right)<=1&&isBalanced(root->left)&&isBalanced(root->right);
        }
    

Log in to reply
 

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