Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum.
The path may start and end at any node in the tree.
Example
Given the below binary tree:
1
/ \
2 3return 6.
Solution
// singlePath: 从root往下走到任意点的最大路径,这条路径可以不包含任何点, 意味着可以直接舍去从根到左或者右的节点,如果为负数直接舍去,这是同Maximum Path Sum II 的区别,后者必须要包含一个跟节点。
// maxPath: 从树中任意到任意点的最大路径,这条路径至少包含一个点
为什么需要这个Singlepath, 函数本身返回任意点到任意点,这意味着,左子树,右子树,经过跟节点的路径,需要单独计算。
Binary Tree Maximum Path Sum II 区别Last updated
Was this helpful?