注意:

// 注意,如果一个类放在另一个类里面,初始化时候会报错 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. [HTML/CSS]display:none和visibility:hidden的区别

    写在前面 在群里有朋友问这样一个问题,display:none的标签,影响了布局.这就引出了本篇这样的问题,印象中display:none的块元素是不占位置的. 一个例子 <!DOCTYPE h ...

  2. 函数式 CSS (FCSS)

    在Wealthfront我们是一个函数式编程的超级粉丝.强调不变性和函数式风格意味着更少的“意外”(surprises),因为副作用是有限的或不存在的.我们能将独立的组件迅速构建出大型系统,通过组合的 ...

  3. linux下命令行查看Memcached运行状态(shell)

    stats查看memcached状态的基本命令,通过这个命令可以看到如下信息:STAT pid 22459                             进程IDSTAT uptime 10 ...

  4. 当你碰到一个网络中有多个PXE Server 肿么办?

    今天在用PXE 安装Openstack Compute节点时,郁闷得发现同一网段中还有一个PXE Server,而我的Compute 启动起来总会先找到它,但那个设置不受我控制,子网也不归我管,那个s ...

  5. codeforces 463D Gargari and Permutations(dp)

    题目 参考网上的代码的... //要找到所有序列中的最长的公共子序列, //定义状态dp[i]为在第一个序列中前i个数字中的最长公共子序列的长度, //状态转移方程为dp[i]=max(dp[i],d ...

  6. Javascript 正则表达式_3

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  7. POJ 1947 Rebuilding Roads(树形DP)

    题目链接 题意 : 给你一棵树,问你至少断掉几条边能够得到有p个点的子树. 思路 : dp[i][j]代表的是以i为根的子树有j个节点.dp[u][i] = dp[u][j]+dp[son][i-j] ...

  8. SVN与CVS的区别大全(转载)

    本节讲解SVN与CVS的区别,主要包括是否更好的冲突标识与处理,是否有更多的本地/离线操作以及元数据管理问题. 更好的冲突标识与处理     通过是否进行更好的冲突标识与处理看SVN与CVS的区别:C ...

  9. Python概述_软件安装_常见问题

    1.  Python安装 目前python有2个大版本,2和3,由于2和3语法有差别,现有的许多库都是基于python2开发,本系列文章以python2为主. 1.1  重要概念 1. 动态语言 运行 ...

  10. Oracle创建用户并赋予权限

    1 CREATE USER username IDENTIFIED BY password; --这个是创建用户(这是最简单的创建语句没有指定表空间) 1 GRANT CREATE SESSION T ...