注意:

// 注意,如果一个类放在另一个类里面,初始化时候会报错 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. Centos最小化安装后联网配置

    Centos最小化安装默认不开启网络,只需进行简单配置就可以上网了. 1.  查看/etc/sysconfig/network-scripts/下面的文件,这里会有一个ifcfg-en******(这 ...

  2. UIResponder类

    UIResponder类 UIResponder类是所有视图类的父类,包括UIView, UIApplication, UIWindow. UIResponder类定义了一些响应和处理事件的方法.事件 ...

  3. HDU1056 HangOver

    HangOver Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Descript ...

  4. HDU1048The Hardest Problem Ever

    The Hardest Problem Ever Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

  5. WinHex分析PE格式(2)&& 如何手动添加区段并运行区段

    由于这次文章内容比较多 所以写成DOC文档 为了复习所学的知识,我在原本的软件里试者手动加入区段 ,并写给大家分享,还试试者用LordPE加区段发现竟然失败了, 还是自己动手比较实在,完美运行. 利用 ...

  6. 谈谈arm下的函数栈

    引言 这篇文章简要说说函数是怎么传入参数的,我们都知道,当一个函数调用使用少量参数(ARM上是少于等于4个)时,参数是通过寄存器进行传值(ARM上是通过r0,r1,r2,r3),而当参数多于4个时,会 ...

  7. 即时通讯UI-聊天界面(UITableView显示左右两人聊天)

    目录 1.创建UITableView对象并设置相关属性 2.创建cellModel模型 //枚举类型 typedef enum { ChatMessageFrom = ,//来自对方的消息 ChatM ...

  8. App接口设计

    关于APP接口设计 http://blog.csdn.net/gebitan505/article/details/37924711/

  9. POJ 2653 Pick-up sticks(线段相交)

    题目链接 题意 : 把每根棍往地上扔,找出最后在上面的棍,也就是说找出所有的没有别的棍子压在它的上面的棍子. 思路 : 对于每根棍子,压在他上面的棍子一定是在它之后扔的棍子,所以在找的时候只要找它之后 ...

  10. kafka配置

    官网:http://kafka.apache.org/ 主要有3种安装方式: 1. 单机单broker 2. 单机多broker 3. 多机多broker 1. wget http://mirror. ...