Description
Given a non-empty binary tree, find the maximum path sum.
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.
Example 1:
1 | Input: [1,2,3] |
Example 2:
1 | Input: [-10,9,20,null,null,15,7] |
解法
嗯,递归解题,但是坑挺多,首先要注意路径不一定从根经过,另外路径必须能够展开成一条直线,这就意味着对于最终路径上的非跟节点必须选择左右子树的一边进行计算,导致递归的返回和进入优先队列的计算值不一样
具体代码如下:
1 | class Solution { |