[抄题]:

给出一棵二叉树,寻找一条路径使其路径和最大,路径可以在任一节点中开始和结束(路径和为两个节点之间所在路径上的节点权值之和)

[思维问题]:

不会写分合法

[一句话思路]:

用两次分治:root2any any2any分一次,左右再分一次。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

先root-any左右各一次,再用any-any。

[一刷]:

  1. left right都是resultType类型,要用到helper函数
  2. root2Any any2Any都不是helper中的变量,需要重新定义:左边或右边的any2any, 递归才是加上中间的any2any

[二刷]:

  1. root为空的corner case中,any2any并不是0,而是MIN_VALUE,保证其它任何数都比它大。
  2. 没有理解递归的实质:a = left.a,一定要出现相同的变量才行
  3. helper函数要有返回的类型

[三刷]:

[四刷]:

[五刷]:

[总结]:

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构,为什么不用别的数据结构]:

[其他解法]:

暴力解法 把所有路径找一遍:n^2

[Follow Up]:

root-leaf 就算有负数也得走:直接左右合并

root-any node 有负数可以不走:max(0,max(left,right)) + root.val 结果要和0比,小于0就只有root.val得了

[LC给出的题目变变变]:

Path Sum 有几条路径和相同:dc

Sum Root to Leaf Numbers:连起来再求和

Univalue Path:最长的相同节点路径

和二叉树有关的,都不能用遍历,要用recursion

/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/*
* @param root: The root of binary tree.
* @return: An integer
*/
class resultType {
int root2Any;
int any2Any;
public resultType (int root2Any, int any2Any) {
this.root2Any = root2Any;
this.any2Any = any2Any;
}
}; private resultType helper (TreeNode root) {
if (root == null) {
return new resultType(0, Integer.MIN_VALUE);
} resultType left = helper(root.left);
resultType right = helper(root.right); int root2Any = Math.max(left.root2Any, right.root2Any) + root.val;
root2Any = Math.max(0, root2Any); int any2Any = Math.max(left.any2Any, right.any2Any);
any2Any = Math.max(any2Any, Math.max(0,left.root2Any) + root.val + Math.max(0,right.root2Any)); return new resultType(root2Any, any2Any);
} public int maxPathSum(TreeNode root) {
return helper(root).any2Any;
}
}

二叉树中的最大路径和 · Binary Tree Maximum Path Sum的更多相关文章

  1. [Swift]LeetCode124. 二叉树中的最大路径和 | Binary Tree Maximum Path Sum

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

  2. 二叉树最大路径和-Binary Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...

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

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

  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 Sum Given a binary tree, find the maximum path sum. The path may start and ...

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

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

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

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

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

随机推荐

  1. (转)android系统开发 AP 和 BP 简要说明

    手机的AP和BP根据上下文可以指代硬件和软件两种意思.  1) 大多数的手机都含有两个处理器.操作系统.用户界面和应用程序都在Application Processor(AP)上执行,AP一般采用AR ...

  2. Web API 源码剖析之默认配置(HttpConfiguration)

    Web API 源码剖析之默认配置(HttpConfiguration) 我们在上一节讲述了全局配置和初始化.本节我们将就全局配置的Configuration只读属性进行展开,她是一个类型为HttpC ...

  3. 详解 Facebook 田渊栋 NIPS2017 论文:深度强化学习研究的 ELF 平台

    这周,机器学习顶级会议 NIPS 2017 的论文评审结果已经通知到各位论文作者了,许多作者都马上发 Facebook/Twitter/Blog/ 朋友圈分享了论文被收录的喜讯.大家的熟人 Faceb ...

  4. 在CentOS7.4中安装jdk的几种方法及配置环境变量

    在CentOS7.4中安装jdk的几种方法及配置环境变量 一.下载jdk jdk下载地址:http://www.oracle.com/technetwork/java/javase/downloads ...

  5. [Python] numpy.random.rand

    numpy.random.rand numpy.random.rand(d0, d1, ..., dn) Random values in a given shape. Create an array ...

  6. 最小生成树-kruskal

    kruskal算法,没有进行算法复杂度分析 判断俩个结点是否在同一个树上使用了dfs,比较low的写法 输入数据 //第一行,结点数,结点数,边数 9 9 14a b 4b c 8c d 7a h 8 ...

  7. Win10交换Ctrl和大写键

    打开注册表 [HKEY_LOCAL_MacHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout] "Scancode Map" ...

  8. linux 使用随笔

    目录:1,ab命令 一,ab命令 ab网站压力测试命令的参数.输出结果的中文注解 ab命令是Apache的Web服务器的性能测试工具,它可以测试安装Web服务器每秒种处理的HTTP请求. 来自: ht ...

  9. 20. orcle中统计一个字符串中某个字符的长度

    例子1:统计一个字符串中“,”的个数: select lengthb(regexp_replace('[a,b,c,d,e,f]','[^,]',null))  as res from dual; 例 ...

  10. quartz 定时任务的增删改

    参考:  https://blog.csdn.net/llmys/article/details/81069863