Just two valid lines C++ code


  • 0
    A
    class Solution {
    public:
        bool isBalanced(TreeNode* root) {
            return root?isBalanced(root->left)&&isBalanced(root->right)&&abs(Depth(root->left)-Depth(root->right))<2:true;
        }
    
        int Depth(TreeNode* node){
            return node?max(Depth(node->left),Depth(node->right))+1:0;
        }
    };

  • 1
    Z

    less code lines does not mean better performance, readability and maintainability are important!


Log in to reply
 

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