Welcome to LeetCode Discuss.  Please read the FAQ to help yourself making the best use of Discuss.
Ask a Question
Back to Problem

Welcome to LeetCode Discuss.

This is a place to ask questions related to only OJ problems.

Please read the FAQ to help yourself making the best use of Discuss.

No empty tree in the testcases

0 votes
967 views

So after I ACed the problem, I looked through the discussion page http://discuss.leetcode.com/questions/288/binary-tree-maximum-path-sum and found that most of the code there does not reset the sum when the tree is empty and if it's empty, they just return Math.MINVALUE/MININT. Then I took a look at the testcases and found it does not include the empty tree case.

I think if it's empty the code should return 0 rather than a min value. Is it a bug or something? Correct me if I'm wrong please.

asked Nov 6, 2013 in Binary Tree Maximum Path Sum by fenghhk (120 points)

1 Answer

+1 vote

No. it should return min. Because, if it returns 0, you cannot figure out there is a path which sums up to 0 or the tree is empty.

answered Nov 6, 2013 by chentao169 (2,050 points)

The logic here is not right. Because if it returns min, you cannot figure out there is a path which sums up to min or the tree is empty. I believe the result of 0 for an empty tree makes sense in the real world also, you just choose a path that includes nothing.


...