注意:

// 注意,如果一个类放在另一个类里面,初始化时候会报错 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)的更多相关文章

  1. [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 ...

  2. 【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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 【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 ...

  7. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

    题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...

  8. 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)

    124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...

  9. [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. ...

  10. 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 ...

随机推荐

  1. while小问题

    while(!m_SMque.pop(data)); 看到这个有点忘了,如果pop返回false会一直执行pop,其实这个执行的是空语句,而while每次执行都需要判断条件,所以如果pop返回fals ...

  2. 连接ACCESS 数据库不能使用 '';文件已在使用中。

    错误类型:  Microsoft JET Database Engine (0x80004005)  不能使用 '':文件已在使用中. 对数据库的操作完之后,要 conn.close() 错误原因:解 ...

  3. javascript陷阱,一不小心你就中招了

  4. 使用 Microsoft Word 发布博客文章

    以 Microsoft Word 2010 为例: 依次选择:文件 -> 保存并发送 -> 发布为博客文章 配置说明:新建账户 的 博客文章 URL  一栏填写 http://rpc.cn ...

  5. Why we have to use epsg:900913 in OpenLayers

    reference:http://docs.openlayers.org/library/spherical_mercator.html epsg:900913 is spicfy the Soher ...

  6. codeforces 442C C. Artem and Array(有深度的模拟)

    题目 感谢JLGG的指导! 思路: //把数据转换成一条折线,发现有凸有凹 //有凹点,去掉并加上两边的最小值//无凹点,直接加上前(n-2)个的和(升序)//数据太大,要64位//判断凹与否,若一边 ...

  7. C#编程使用Managed Wifi API连接无线SSID

    C#编程使用Managed Wifi API连接无线SSIDhttp://www.2cto.com/kf/201307/227623.html Managed Wifi API - Homehttp: ...

  8. Python之socketserver源码分析

    一.socketserver简介 socketserver是一个创建服务器的框架,封装了许多功能用来处理来自客户端的请求,简化了自己写服务端代码.比如说对于基本的套接字服务器(socket-based ...

  9. C#中的 序列化和反序列化

    什么是序列化和反序列化? 序列化就是把一个对象保存到一个文件或数据库字段中去,反序列化就是在适当的时候把这个文件再转化成原来的对象使用. 我想最主要的作用有: 1.在进程下次启动时读取上次保存的对象的 ...

  10. 关于在linux下清屏的几种技巧

    在windows的DOS操作界面里面,清屏的命令是cls,那么在linux 里面的清屏命令是什么呢?下面笔者分享几种在linux下用过的清屏方法. 1.clear命令.这个命令将会刷新屏幕,本质上只是 ...