问题描述:
https://oj.leetcode.com/problems/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.For example:
1 / \ 2 3
Return 6
.
问题分析:
定义:
给定二叉树root,path sum的定义是 从开始节点start 到结束节点end 所有节点的值的加和。其中start和end可以是二叉树中的任意节点。二叉树是递归定义,所有二叉树相关的问题都可以考虑采用递归的设计方案。定义一个新的求和方式: end 节点必须是root节点。在该种情况下最大和的计算只有3种情况:
1.root -val节点 2. root ->val+ maxSum(root->left) 3. root->val +maxSum( root->right)
有了上面的定义之后再计算max path sum 就容易了,比较像最大子数组的和计算。