124. Binary Tree Maximum Path Sum
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.
1 | For example: |
题意:
给出一个二叉树,找出最大路径的总和。
对于这个问题,路径被定义为从一个起始节点到树中的任何节点沿着父子关系连接的任意序列。注意:路径不需要经过根。
思路:
主要是理解题意,就是查找从一个节点到其左右子树中最大路径值得和。
需要考虑以上两种情况:
1 左子树或者右子树中存有最大路径和不能和根节点形成一个路径
2 左子树 右子树 和根节点形成最大路径
1 | class Solution { |