Clean C++ recursive solution


  • 0
        int findTilt(TreeNode* root) {
            if(!root) return 0;
            int leftSum=DFS(root->left);
            int rightSum=DFS(root->right);
            int tilt=abs(leftSum-rightSum);
            return tilt+findTilt(root->left)+findTilt(root->right);
        }
        
        int DFS(TreeNode* root){
            if(!root) return 0;
            return root->val+DFS(root->left)+DFS(root->right);
        }
    

Log in to reply
 

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