Binary Tree Maximum Path Sum leetcode java
题目:
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.
题解:
递归求解。
取当前点和左右边加和,当前点的值中最大的作为本层返回值。并在全局维护一个max。使用数组,因为是引用类型。所以在递归过程中可以保存结果。
代码如下:
 1     public int maxPathSum(TreeNode root) {
 2         int[] max = new int[1];
 3         max[0] = Integer.MIN_VALUE;
 4         findmax(root,max);
 5         return max[0];
 6     }
 7     
 8     public int findmax(TreeNode root, int[] max){
 9         if(root==null)
             return 0;
         
         int left = findmax(root.left,max);
         int right = findmax(root.right,max);
         
         int ans = Math.max(root.val,Math.max(root.val+left, root.val+right));
         
         max[0] = Math.max(max[0],Math.max(ans,root.val+left+right));
         
         return ans;
         
     }												
											Binary Tree Maximum Path Sum leetcode java的更多相关文章
- Binary Tree Maximum Path Sum - LeetCode
		
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
 - Binary Tree Maximum Path Sum [leetcode] dp
		
a(i):在节点i由于单边路径的最大结束 b(i):在节点i路径和 a(i) = max{ i->val, i->val + max{a(i->left), a(i->righ ...
 - [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 SumGiven a binary tree, find the maximum path sum. The path may start and e ...
 - 【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 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】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 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
 
随机推荐
- BZOJ2465: [中山市选2009]小球
			
Description 给定n个不同颜色的球,每个球都有一个分数,同时有m个瓶子,每个瓶子都有固定的容量.现在,你必须把球放到瓶子里面.请编程计算最多能放多少个球到这些瓶子里. Inpu ...
 - 喵哈哈村的魔法考试 Round #5 (Div.2) 题解
			
老规矩 有问题直接联系我:475517977@qq.com A 直接暴力的for一遍,统计连续的有多少个就好了.模拟题. #include<bits/stdc++.h> using nam ...
 - SWD Connect/Transfer Source Code
			
Serial Wire Debug interface The Serial Wire Debug protocol operates with a synchronous serial interf ...
 - [Go] 第一个单词首字母变大写:Ucfirst(),第一个单词首字母变小写:Lcfirst()
			
import ( "unicode" ) func Ucfirst(str string) string { for i, v := range str { return stri ...
 - ASP.NET Web API实践系列06, 在ASP.NET MVC 4 基础上增加使用ASP.NET WEB API
			
本篇尝试在现有的ASP.NET MVC 4 项目上增加使用ASP.NET Web API. 新建项目,选择"ASP.NET MVC 4 Web应用程序". 选择"基本&q ...
 - Spring boot配置多个Redis数据源操作实例
			
原文:https://www.jianshu.com/p/c79b65b253fa Spring boot配置多个Redis数据源操作实例 在SpringBoot是项目中整合了两个Redis的操作实例 ...
 - 安装oracle 11g环境变量ORACLE_HOME的一个问题 转
			
http://blog.itpub.net/26129555/viewspace-1243467/报错内容: OUI-10137:An Oracle Home with name ORACLE_HOM ...
 - WordPress主题开发:get_term_by和get_term_link
			
学习目的: 某一个分类的名称.别名.和id都可以到后台自己去找,但这样找比较麻烦还容易看错,wordpress提供了下面两个函数get_term_by和get_term_link,只要提供别名.名称或 ...
 - 从Oracle迁移到MySQL的各种坑及自救方案
			
当企业内部使用的数据库种类繁杂时,或者有需求更换数据库种类时,都可能会做很多数据迁移的工作.有些迁移很简单,有些迁移可能就会很复杂,大家有没有考虑过为了顺利完成复杂的数据库迁移任务,都需要考虑并解决哪 ...
 - IP地址和子网划分学习笔记之《预备知识:进制计数》
			
一.序:IP地址和子网划分学习笔记开篇 只要记住你的名字,不管你在世界的哪个地方,我一定会去见你.——新海诚 电影<你的名字> 在我们的日常生活中,每个人的名字对应一个唯一的身(敏)份(感 ...