binary-tree-maximum-path-sum Java code
来源:互联网 发布:淘宝手机助手苹果版 编辑:程序博客网 时间:2024/05/18 09:27
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
Return6.
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int maxPathSum(TreeNode root) { //递归 if(root==null){ return 0; } int[] res={Integer.MIN_VALUE}; helper(root,res); return res[0]; } public int helper(TreeNode root,int[] res){ int left=0; int right=0; if(root.left!=null){ left=helper(root.left,res); left=left<0?0:left; } if(root.right!=null){ right=helper(root.right,res); right=right<0?0:right; } res[0]=Math.max(root.val+left+right,res[0]); return root.val+Math.max(left,right); }}
阅读全文
0 0
- binary-tree-maximum-path-sum Java code
- CODE 9: Binary Tree Maximum Path Sum
- [Leetcode] Binary Tree Maximum Path Sum (Java)
- Binary Tree Maximum Path Sum Java
- Binary Tree Maximum Path Sum (Java)
- [LeetCode][Java] Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum[leet code test cases passed]
- leetcode 日经贴,Cpp code -Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- SXOJ844 牛的基因组
- 计算机视觉与图像处理、模式识别、机器学习学科之间的关系
- jsonrpc python实现
- 嵌入式Linux+Android学习路线图+学习进度
- Android 7.1 PopupWindow适配问题
- binary-tree-maximum-path-sum Java code
- Python3.6 20个入门级小程序(五)
- 超链接有哪几种 制作表格时WPS怎么在表格中插入超链接
- linux下安装MySQL数据库5.1.73
- 1007. Maximum Subsequence Sum (25)
- QT+Cmake 构建C++工程,调用PCL库,可调试
- xxx不是内部或外部命令,也不是可运行的程序
- printf函数结合自增自减运算的使用
- Qt:播放动态gif图片