124. Binary Tree Maximum Path Sum
124. Binary Tree Maximum Path Sum
problem 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:
Example 2:
algorithm thought
还是典型的树问题,使用递归解决。
这里题目有点描述不清楚,这里只有根节点可以连接两个子树,也就是加上子树上计算的结果。但是如果是中间节点,那就只能连接一个子树,选择左子树或者右子树。这里当然是选择最大的一个子树。
code
algorithm analysis
对所有的节点遍历一遍得出结果,时间复杂度O(n)
Last updated