Given a binary tree, find the maximum path sum.
The path may start and end at any node in the tree.
For example:
Given the below binary tree,
1 / \ 2 3
Return 6
.
题意:对于给定二叉树,查找最长路径和。该路径的起点,终点可以是二叉树的任意一个节点。
分类:二叉树
解法1:以采用Binary Tree最常用的dfs来进行遍历。先算出左右子树的结果L和R,如果L大于0,那么对后续结果是有利的,我们加上L,如果R大于0,对后续结果也是有利的,继续加上R。
原文链接http://blog.youkuaiyun.com/crazy__chen/article/details/46836605