C++ Two Function 4 line Solution


  • 0
    W

    C++ Two Function 4 line Solution

    class Solution {
    public:
        int countChild(TreeNode* node) {
            if (node == nullptr) return 0;
            return node->val + countChild(node->left) + countChild(node->right);
        }
    
        int findTilt(TreeNode* root) {
           if (root == nullptr) return 0;
           return abs(countChild(root->left) - countChild(root->right)) + findTilt(root->left) + findTilt(root->right);
        }
    };
    

Log in to reply
 

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