124. Binary Tree Maximum Path Sum

https://leetcode.com/problems/binary-tree-maximum-path-sum/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:

Input: [1,2,3]

       1
      / \
     2   3

Output: 6

Example 2:

Input: [-10,9,20,null,null,15,7]

   -10
   / \
  9  20
    /  \
   15   7

Output: 42

Thoughts

二叉树上返回和最大的路径和。和diameter异曲同工。分治,global max记录全局最优, helper返回从下往上且包含当前节点的能继续往上延伸的最大sum。全局最优可能是同时包含左右两支。由于负数的存在,返回时node.val+max(l, r, 0),表示局部最优是负数时最佳选择是不选子结点。

Code

Analysis

每个节点遍历一次, 时间复杂度O(N).

Ver.2

用区分backtracking的Iterative写。 当pop()后, peek()得到的是pop的父节点.和分治一样直接利用pop()时对应的preRes. 如果还有右子树没处理, 则不pop父节点, 而同样处理右子树. 注意这时preRes需要清零, 因为preRes存的还是左子树的结果, 右子树肯定不能直接用到左子树的结果。

Last updated

Was this helpful?