binary-tree-maximum-path-sum(mock)
注意:
// 注意,如果一个类放在另一个类里面,初始化时候会报错 Solution is not a enclosing class
// 这是因为如果TreeNode不是static,那么要求先有外部类的实例
// 要加上static
// 或者放到类的外面
https://leetcode.com/problems/binary-tree-maximum-path-sum/
https://leetcode.com/mockinterview/session/result/xslp8c2/
package com.company; import java.util.ArrayList;
import java.util.List; class Solution { // 注意,如果放在Solution里面,会报错 Solution is not a enclosing class
// 这是因为如果TreeNode不是static,那么要求先有外部类的实例
// 要加上static
static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
} public int maxPathSum(TreeNode root) {
// 要求至少有一个元素,全是负数情况下不能认为是0
if (root == null) {
return 0;
}
List<Integer> ret = impl(root);
return ret.get(1);
} // 多值返回一般放在容器里
// [0]包含root的单一路径最大值; [1] 最大值;
// 调用时保证root不会为null
private List<Integer> impl(TreeNode root) {
List<Integer> ret = new ArrayList();
int maxWithRoot = root.val;
int maxRet = root.val; if (root.left != null) {
List<Integer> left = impl(root.left);
System.out.printf("Here is left %d, ret: %d, %d\n", root.left.val, left.get(0), left.get(1));
if (left.get(0) > 0) {
maxWithRoot = root.val + left.get(0);
}
maxRet = maxWithRoot > left.get(1) ? maxWithRoot : left.get(1); }
if (root.right != null) {
List<Integer> right = impl(root.right);
int tmp = maxWithRoot;
if (root.val + right.get(0) > maxWithRoot) {
maxWithRoot = root.val + right.get(0);
}
// 下面这个地方因为考虑不周,导致了一个bug,只考虑了maxWithRoot,没有考虑之前的maxRet
maxRet = maxWithRoot > maxRet ? maxWithRoot : maxRet;
maxRet = maxRet > right.get(1) ? maxRet : right.get(1); // merge two branch
if (tmp + right.get(0) > maxRet) {
maxRet = tmp + right.get(0);
}
} ret.add(maxWithRoot);
ret.add(maxRet);
System.out.printf("Here is node %d, ret: %d, %d\n", root.val, maxWithRoot, maxRet);
return ret;
}
} public class Main { public static void main(String[] args) {
// write your code here
System.out.println("Hello"); Solution.TreeNode node1 = new Solution.TreeNode(1);
Solution.TreeNode node2 = new Solution.TreeNode(2);
Solution.TreeNode node3 = new Solution.TreeNode(3);
Solution.TreeNode node4 = new Solution.TreeNode(4);
Solution.TreeNode node5 = new Solution.TreeNode(5);
Solution.TreeNode node6 = new Solution.TreeNode(6);
Solution.TreeNode node7 = new Solution.TreeNode(7);
Solution.TreeNode node8 = new Solution.TreeNode(8);
Solution.TreeNode node9 = new Solution.TreeNode(9);
Solution.TreeNode node10 = new Solution.TreeNode(10);
node1.left = node2;
node1.right = node3;
node2.left = node4;
node2.right = node5;
node3.left = node6;
node3.right = node7;
node4.left = node8;
node4.right = node9;
node5.left = node10; Solution solution = new Solution();
int ret = solution.maxPathSum(node1);
System.out.printf("Get ret: %d\n", ret); }
}
binary-tree-maximum-path-sum(mock)的更多相关文章
- [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 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- [LeetCode] 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(124) Binary Tree Maximum Path Sum
题目 Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequen ...
随机推荐
- nginx+php+flight 构建RESTFul API
配置: Nginx: conf目录下nginx.conf配置文件. 第44行改为:root D:/wwwroot/www; 第45行改为:index index.html index.htm i ...
- AngularJs学习笔记--Guide教程系列文章索引
在很久很久以前,一位前辈向我推荐AngularJs.但当时我没有好好学习,仅仅是讲文档浏览了一次.后来觉醒了……于是下定决心好好理解这系列的文档,并意译出来(英文水平不足……不能说是翻译,有些实在是看 ...
- Sqli-labs less 29
Less-29 首先先看下tomcat中的index.jsp文件 在apache的index.php中,sql语句为 $sql="SELECT * FROM users WHERE id=' ...
- 解决Myeclipse 编辑jsp页面卡
解决Myeclipse 编辑jsp页面卡 编辑一个jsp页面时,如果每输入一下,CPU都100%一下,和大家分项一下. 问题: 当你编辑一个jsp页面时,如果每输入一下,CPU都100%一下,3, ...
- call by reference and copy/restore
转自:http://stackoverflow.com/questions/8848402/whats-the-difference-between-call-by-reference-and-cop ...
- php laravel 安装
windows环境尝试学习一下laravel 1.因为SAE的php版本为5.3,因此最高只能支持到Laravel4.1.x.(Laravel4.2用到了php5.4的trait特性) 以4.1为主. ...
- iOS模型以及使用
个人习惯,也可以不这样写 创建模型基类: #import <Foundation/Foundation.h> @interface WJBaseModel : NSObject //将字典 ...
- Git 10 周年之际,创始人 Linus Torvalds 访谈
点这里 十年前的这一周,linux 内核社区面临一个根本性的挑战:他们不再能够使用他们的修复控制系统:BitKeeper,同时其他的软件配置管理遇到了对分布式系统的新需求.Linus Torvalds ...
- PHP获取http头信息和CI中获取HTTP头信息的方法
CI中获取HTTP头信息的方法: $this->input->request_headers() 在不支持apache_request_headers()的非Apache环境非常有用.返回 ...
- IDTHook 深入学习
在之前的一篇文章中介绍了替换IDT向量表中的地址来达到Hook的目的 IDT hook KiTrap03 但是这样很容易就可以被检测了.接下来要学习就是通过patch GDT来达到Hook IDT的目 ...