Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
386 B
2
Indexable
lass Solution {
public:
    int solve(TreeNode* root,int ans){
        if(root==NULL) return ans;

        //ans=max(ans,root->val);
        int left =solve(root->left,ans+root->val);
        int right=solve(root->right,ans+root->val);
        return max(left,right);

    }
    int maxPathSum(TreeNode* root) {
        int ans=0;
        return solve(root,ans);
    }
};