[lintcode] Binary Tree Maximum Path Sum II
Given a binary tree, find the maximum path sum from root.
The path may end at any node in the tree and contain at least one node in it.
给一棵二叉树,找出从根节点出发的路径中,和最大的一条。
这条路径可以在任何二叉树中的节点结束,但是必须包含至少一个点(也就是根了)。
- /**
- * Definition of TreeNode:
- * public class TreeNode {
- * public int val;
- * public TreeNode left, right;
- * public TreeNode(int val) {
- * this.val = val;
- * this.left = this.right = null;
- * }
- * }
- */
- public class Solution {
- /**
- * @param root the root of binary tree.
- * @return an integer
- */
- public int maxPathSum2(TreeNode root) {
- if (root == null) {
- return Integer.MIN_VALUE;
- }
- int left = maxPathSum2(root.left);
- int right = maxPathSum2(root.right);
- return root.val + Math.max(0, Math.max(left, right));
- }
- }
[lintcode] Binary Tree Maximum Path Sum II的更多相关文章
- LintCode 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. ...
- [leetcode]Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- 【leetcode】Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- 26. Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)
124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...
- LeetCode: Binary Tree Maximum Path Sum 解题报告
Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and e ...
- 【LeetCode】124. Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum
题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...
- 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
随机推荐
- 功能完善的Java连接池调用实例
/** * Title: ConnectPool.java * Description: 连接池管理器 * Copyright: Copyright © 2002/12/25 * Company: * ...
- webstorm9.3 安装less 编译css教程
第一步:安装node.js webstrom9.3汉化下载:http://pan.baidu.com/s/1ntGNNiL 密码:1fbh node.js 下载地址:https://nodejs ...
- Module模式
<script> var myModel=(function(){ var model={}; var privateVar="Hello World"; functi ...
- winform的tab跳到下一个
先设置TabStop=true,再设置TabIndex的顺序
- UINT数相减
UINT32 i = ; UINT32 j = ; i - j > //这个将永远为真,因为他是将将结果按照无符号解析 int = i -j; //这个是-1,是按照有符号解析 今天发现代码里面 ...
- php操作redis
redis的操作很多的,以前看到一个比较全的博客,但是现在找不到了.查个东西搜半天,下面整理一下php处理redis的例子,个人觉得常用一些例子.下面的例子都是基于php-redis这个扩展的. 1, ...
- Iframe 在项目中的使用总结
参考:http://www.cnblogs.com/MaxIE/archive/2008/08/13/1266597.html 问题一:首先我们用iframe加载页面,第一个需要解决的问题是高度自适应 ...
- jquery 停止animate动画,并且回复最初状态
// 热门推荐悬浮效果 $("#recom_con li img").mouseenter(function(){ $(this).stop(true, true); $w = p ...
- PetaPoco dynamic
PetaPoco最初的灵感来自Massive-通过dynamic Expando objects返回一切.对于大多数情况我觉得这比较麻烦,更喜欢强类型的类.但是有些时候支持dynamic也是有用的-特 ...
- Python之路【第九篇】堡垒机基础&数据库操作
复习paramiko模块 Python的paramiko模块,是基于SSH用于连接远程服务器并执行相关操作. SSHClient #!/usr/bin/env python #-*- coding:u ...