leetcode Binary Tree Maximum Path Sum
来源:互联网 发布:非农数据最新消息 编辑:程序博客网 时间:2024/06/13 12:22
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
.
需要设置一个全局变量max来更新path sum的最大值,因为max path可能发生在任何点和路径,代码:
private int max=Integer.MIN_VALUE;public int maxPathSum(TreeNode root) { getMax(root); return max;}public int getMax(TreeNode root){ if(root==null) return 0; int val=root.val; int leftMax=getMax(root.left); if(leftMax>0) val+=leftMax; int rightMax=getMax(root.right); if(rightMax>0) val+=rightMax; if(val>max) max=val; return Math.max(root.val+leftMax,Math.max(root.val+rightMax,root.val));}
0 0
- *****(leetcode) Binary Tree Maximum Path Sum (tree)
- LeetCode Binary Tree Maximum Path Sum
- [Leetcode] Binary Tree Maximum Path Sum
- [leetcode] Binary Tree Maximum Path Sum
- LeetCode Binary Tree Maximum Path Sum
- [LeetCode] Binary Tree Maximum Path Sum
- leetcode Binary Tree Maximum Path Sum
- 【leetcode】Binary Tree Maximum Path Sum
- LeetCode - Binary Tree Maximum Path Sum
- [LeetCode]Binary Tree Maximum Path Sum
- [Leetcode]Binary Tree Maximum Path Sum
- Leetcode: Binary Tree Maximum Path Sum
- LeetCode-Binary Tree Maximum Path Sum
- [leetcode] Binary Tree Maximum Path Sum
- LeetCode - Binary Tree Maximum Path Sum
- LeetCode 124: Binary Tree Maximum Path Sum
- 【Leetcode】Binary Tree Maximum Path Sum
- LeetCode | Binary Tree Maximum Path Sum
- FairyGUI
- Codeforces 233C Partial Sums(特殊三角矩阵压缩)
- java.io.IOException: Attempted read from closed stream.
- 欢迎使用CSDN-markdown编辑器
- 在SurfaceView中实现覆盖刷新和脏矩形刷的方法
- leetcode Binary Tree Maximum Path Sum
- UVALive LA3905 Meteor (扫描线统计)
- 进程间通信-信号量
- 安卓探究纵深博客
- GeokoFx (1)替换 webbrowser ,环境搭建
- iOS开发实现TouchID指纹解锁
- SDWebImage加载图片设置cookie
- HDU2866 Special Prime
- QCustomPlot之绘图的基础