Binary Tree Maximum Path Sum
来源:互联网 发布:网络黑侠现在在哪写书 编辑:程序博客网 时间:2024/05/19 20:19
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
.
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { private int maxSum; public int maxPathSum(TreeNode root) { maxSum = Integer.MIN_VALUE; findMax(root); return maxSum; } private int findMax(TreeNode p) { if (p == null) return 0; int left = findMax(p.left); int right = findMax(p.right); maxSum = Math.max(maxSum, left + right + p.val); int ret = Math.max(left, right) + p.val; return ret > 0 ? ret:0; }}
0 0
- 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
- 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
- C++ string学习
- Java语言描述:递归与分治策略之合并排序与快速排序
- Visual C++学习笔记1:一定要注意ANSI和UNICODE的区别
- 命令行方式安装Python第三方工具包
- 第一篇博客
- Binary Tree Maximum Path Sum
- 设计模式那点事--单例模式
- 数值优化(Numerical Optimization)学习系列-线搜索方法(LineSearch)
- POJ - 1422 Air Raid 二分图最大匹配
- Css浮动的含义和div卡住状况的分析
- [php] php纯静态化简单实现
- C++的一些感想
- hdu 5265 pog loves szh II
- 2_opencv2计算机视觉学习_操作像素