124. Binary Tree Maximum Path Sum
来源:互联网 发布:淘宝售后客服话术大全 编辑:程序博客网 时间:2024/06/05 20:08
1.定义maxDeep(x)为以x为根节点的树的max path
maxDeep(x)等于在下面a,b,c中找到一个最大的值,c的话是因为左子树的max path 和 右子树的max path可能值为负
a.左子树的max path + x自身的值
b.右子树的max path + x自身的值
c.x自身的值
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { int ans = Integer.MIN_VALUE; int maxDeep(TreeNode root){ if(root == null) return 0; int curAns = root.val; int lMax = maxDeep(root.left); int rMax = maxDeep(root.right); if(lMax > 0 ){ curAns += lMax; } if(rMax >0){ curAns += rMax; } if(curAns > ans){ ans = curAns; } return Math.max(root.val,Math.max(root.val+lMax,root.val+rMax)); } public int maxPathSum(TreeNode root) { maxDeep(root); return ans; }}
阅读全文
0 0
- Tree-----124. Binary Tree Maximum Path Sum
- LeetCode 124. Binary Tree Maximum Path Sum
- [LeetCode]*124.Binary Tree Maximum Path Sum
- [Leetcode] 124. Binary Tree Maximum Path Sum
- [leetcode] 124. Binary Tree Maximum Path Sum
- 124. Binary Tree Maximum Path Sum
- [leetcode] 124. Binary Tree Maximum Path Sum
- Leetcode 124. Binary Tree Maximum Path Sum
- Leetcode 124. Binary Tree Maximum Path Sum
- LeetCode 124. Binary Tree Maximum Path Sum
- leetcode 124. Binary Tree Maximum Path Sum
- LeetCode 124. Binary Tree Maximum Path Sum
- 124. Binary Tree Maximum Path Sum
- 124. Binary Tree Maximum Path Sum
- Leetcode 124. Binary Tree Maximum Path Sum
- [LeetCode] 124. Binary Tree Maximum Path Sum
- 124. Binary Tree Maximum Path Sum
- leetcode 124.Binary Tree Maximum Path Sum
- LCA 问题的倍增解法
- Form身份验证基本原理
- VMware安装Ubuntu17.04的VMware-tools提示:what is the location of the "ifconfig" program on your machin问题解决
- 什么叫抽象类什么叫抽象方法
- webpack配置入口文件和目标文件
- 124. Binary Tree Maximum Path Sum
- 数据库性能测试方案示例
- JStorm与Storm源码分析(二)--任务分配,assignmen
- SQL视频总结(2)
- elasticsearch-jdbc-2.3.3 安装
- 1990-数组排序
- Greedy Match学习笔记二 —— 安慰剂检验与置信区间计算
- J2EE学习篇之--JDBC详解
- MyBatis学习笔记(三) 关联关系