Binary Tree Maximum Path Sum
来源:互联网 发布:知乎还是百度知道 编辑:程序博客网 时间:2024/04/29 11:14
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 binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { int max; public int maxPathSum(TreeNode root) { // Start typing your Java solution below // DO NOT write main() function // max = (root==null)? 0: root.val; if(root == null) { return 0; } max = Integer.MIN_VALUE; 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(root.val + left + right, max); return root.val + Math.max(left,right); }}
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
- error 1935 安装程序集出错
- Android MuPDF 部署
- IIS7服务器的配置(win7,支持asp.net)
- java字符串——字符串的分割split()
- [回溯]Prime Ring Problem UVA524
- Binary Tree Maximum Path Sum
- 斯蒂芬森
- MD5校验码
- Linux系统修改主机名与IP设置
- CSS3 Transitions, Transforms和Animation使用简介与应用展示
- linux tar.gz zip 解压缩 压缩命令
- Android打开系统相册的问题
- AOF - Redis 设计与实现
- c# 字符串按规定长度拆分