【小熊刷题】Binary Tree Maximum Path Sum
来源:互联网 发布:mac python如何加载库 编辑:程序博客网 时间:2024/04/29 23:55
Question
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 4
/ \2 3
The highlighted path yields the maximum sum 10.
https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/
*Difficulty: Hard, Frequency: Medium
Solution
/** * 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; } public int findMax(TreeNode node){ if(node == null) return 0; int left = findMax(node.left); int right = findMax(node.right); maxSum = Math.max(node.val+left+right, maxSum); int ret = Math.max(right, left) + node.val; return ret > 0 ? ret : 0; }}
0 0
- 【小熊刷题】Binary Tree Maximum Path Sum
- [leetcode刷题系列]Binary Tree Maximum Path Sum
- LeetCode刷题笔录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++虚继承的内存模型
- CAP理论和BASE模型
- class和struct的区别
- eclipse java中路径问题
- 【POJ2031】【最小生成树】【g++ f c++ lf】
- 【小熊刷题】Binary Tree Maximum Path Sum
- 【算法导论】分治策略
- hdu 4628 Pieces 状态压缩DP
- C++指针加整数、两个指针相减的问题
- 交叉编译ARM版 Qt (qt-everywhere-opensource-src-4.7.3)
- 一个Angularjs实际项目的开发历程微博
- 小议:Site Collection中SharePoint Publishing Feature无法激活的解决方案
- 自定义类似Gridview 不同行列数不同 的显示控件
- Highmaps网页图表教程之绘图区显示标签显示数据标签定位