Leetcode no. 124
来源:互联网 发布:北京赛车pk10计划软件 编辑:程序博客网 时间:2024/06/04 18:52
124. Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum.
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root.
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 { public int maxPathSum(TreeNode root) { return iterate(root)[1]; } private int[] iterate(TreeNode root){ int[] res= {0, Integer.MIN_VALUE}; if (root== null) return res; int[] left= iterate(root.left); int[] right= iterate(root.right); int Single= Math.max(left[0], right[0])+ root.val; res[0]= Math.max(Single, 0); int Max= Math.max(left[1], right[1]); res[1]= Math.max(Max, left[0]+right[0]+root.val); return res; }}
0 0
- Leetcode no. 124
- LeetCode No.11 ~No.12
- Leetcode NO.78 Subsets
- Leetcode NO.77 Combinations
- Leetcode NO.46 Permutations
- 【leetcode】No. 235 LCABST
- LeetCode no. 35
- Leetcode no. 37
- Leetcode no. 279
- Leetcode no. 335
- Leetcode no. 283
- Leetcode no. 181 (sql)
- Leetcode no. 40
- Leetcode no. 238
- Leetcode no. 26
- Leetcode no. 80
- Leetcode no. 228
- Leetcode no. 313
- POJ 1182 洛谷 P2024 食物链
- GithubPages教程 在GithubPages上搭建项目主页
- 使用Properties读写属性文件
- PAT-B 1050. 螺旋矩阵
- this & super
- Leetcode no. 124
- 输出3个数中的最大值(2016-06-18)
- HTMl笔记
- C++ 重载决议overload resolution 与 SFINAE
- macOS Sierra内置功能更新(续)-已知其它
- VC/MFC之ListCtrl控件使用经验总结
- MFC遍历/清空所有Edit控件内容
- 2016年截止6月17日以来指引文档(Guide)的更新列表(Apple)
- IOS的代理模式