Why is checking value of the current node necessary?

  • 1

    I found the solution to this problem in the
    old leet code discuss forum.

    I don't understand this line of code:

     csum = max(node->val, max(node->val + lsum, node->val + rsum));

    Why do we need to check if current node is greater than the sum of itself and its left and right subtree? Is it because a binary tree can contain negative numbers? If it contained only positive numbers, that check would be unnecessary, right? Or am I completely wrong here?

  • 2

    I think you are right.

    Some of the test cases do contain negative numbers.

Log in to reply

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