题目
原题页面:https://leetcode.com/problems/binary-tree-maximum-path-sum/
题目类型:Tree, Depth-first Search
难度评价:Hard
类似题目:(M) Path Sum, (M) Sum Root to Leaf Numbers
Given a 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 does not need to go through the root.
For example:
Given the below binary tree,1 / \ 2 3Return
6.
分析
本是比较简单的一道二叉树递归算法题目,也花了一些时间。主要还是在题意的理解中,本题要求的“最大路径和”,不一定经过了root,起点和终点可以是同一个点(即整个路径只有一个点)。
注意用一个类成员变量max来记录搜索过程中出现过的最大路径和,最后需要返回的就是这个max值。
此外还有一点容易搞错,当一棵子树计算完退栈时,向上一层返回的应该是该子树左路径、右路径中的最大值,而不是该子树中的“最大路径和”,因为从父节点搜索进入该子树时,只能选择其左分支或者右分治进入。
最后需要注意的一点是,这道题目并没有说节点的值是正数或者自然数,考虑到leetcode一贯的尿性,最好认为负数也是会出现的。
代码
1 | class Solution: |
本文地址:https://xnerv.wang/leetcode-binary-tree-maximum-path-sum/
