Max Sum Path in Binary Tree
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
int res; int dfs(TreeNode* A) { if(A == NULL) return 0; int l = dfs(A->left), r = dfs(A->right); res = max(res, l + r + A->val); res = max(res, max({l,r,0}) + A->val); return max({l,r,0}) + A->val; } int Solution::maxPathSum(TreeNode* A) { res = INT_MIN; dfs(A); return res; }
|