[Leetcode][JAVA] 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:
Given the below binary tree,
1
/ \
2 3
Return 6.
对于树,我们可以找到其左右子树中终止于根节点的最大路径值,称为最大半路径值,如果都是正数,则可以把它们以及根节点值相加,如果其中有负数,则舍弃那一边的值(即置为零)。如此可以找到包含根节点的最大路径值。然后选择左右子树的最大半路径值中大的那个(负数则置为0)加上根节点的值作为新的最大半路径值传给父节点。
于是我们可以递归地考察每个子树,不断更新一个全局变量max。
基本情况为:子树为null,此时最大路径值应为0.
代码如下:
int max;
public int maxPathSum(TreeNode root) {
max = root==null?0:root.val;
findMax(root);
return max;
}
public int findMax(TreeNode root) {
if(root==null)
return 0;
int left = Math.max(findMax(root.left),0);
int right = Math.max(findMax(root.right),0);
max = Math.max(max, left+right+root.val);
return Math.max(left, right) + root.val;
}
[Leetcode][JAVA] Binary Tree Maximum Path Sum的更多相关文章
- 【leetcode】Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)
124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...
- 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- LeetCode 124. Binary Tree Maximum Path Sum 二叉树中的最大路径和 (C++/Java)
题目: Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as ...
- Java for LeetCode 124 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. ...
- leetcode 124. Binary Tree Maximum Path Sum ----- java
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- leetcode@ [124] Binary Tree Maximum Path Sum (DFS)
https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ...
- [leetcode]124. Binary Tree Maximum Path Sum二叉树最大路径和
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
- [LeetCode] 124. Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
随机推荐
- net.sf.json 时间格式的转化
后台代码 //后台代码 response.setCharacterEncoding("UTF-8"); JsonConfig jsonConfig = new JsonConfig ...
- Asp.net 怎样去除表单多行文本框滚动条
<textarea style="overflow:hidden;border-width:0px;">永远没有滚动条</textarea><text ...
- node socket.io web
soket.io & web http://socket.io/get-started/chat/ 新建一個文件夾 soketWeb ; 在sokertWeb 文件夾內新建一個 package ...
- 关于隐藏input输入内容问题
如果想通过获取焦点输入改变内容,type不能是hidden的 <input type="hidden" id="test"> // 这种是不行的,只 ...
- Search history in "Maps"
A friend of mine came to me with her iPhone yesterday. She wanted to know how to clear search histor ...
- ORACLE SQL前端补0的三种方式。
前端补0的三种方式. select lpad(sal,8,'0') from emp;select to_char(sal,'00000000') from emp;select substr('00 ...
- Find Query Window的运作(手电筒)
Find Query Window的運作?(手电筒) 提示: 在點選 Toolbar的 Find鈕時,系統會觸發 Query_Find此 Trigger. 執行 App_Find.Query_Find ...
- centos7.x/RedHat7.x重命名网卡名称
从51CTO博客迁移出来几篇博文. 在CentOS7.x或RedHat7.x上,网卡命名规则变成了默认,既自动基于固件.拓扑结构和位置信息来确定.这样一来虽然有好处,但也会影响操作,因为新的命名规则比 ...
- 自定义底部tab
public class MainActivity extends TabActivity implements OnCheckedChangeListener { private RadioGrou ...
- afterTextChanged() callback being called without the text being actually changed
afterTextChanged() callback being called without the text being actually changed up vote8down votefa ...