[抄题]:

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

[思维问题]:

不会写分合法

[一句话思路]:

用两次分治: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. Server Tomcat v8.0 Server at localhost failed to start.的解决方法

    1.可能是web.xml中的filter-mapping中url-pattern没加/* 2.可能是servlet和servlet-mapping中的servlet-name不匹配

  2. [UE4]为什么会有类型检查

    类型检查 定义:对象的行为是否符合类型的行为 作用:帮助开发者找出潜在的错误. 类型转换 隐式类型转换:整数可以和浮点数运算 显式类型转换/强制类型转换

  3. freePBX汉化方法记录——备忘

    FreePBX汉化[root@bgcc69:/var/www/html/admin/i18n/zh_CN/LC_MESSAGES]$pwd/var/www/html/admin/i18n/zh_CN/ ...

  4. 【BZOJ】1013 [JSOI2008]球形空间产生器sphere(高斯消元)

    题目 传送门:QWQ 分析 高斯消元就是个大暴力.... 代码 #include <bits/stdc++.h> using namespace std; ; ; int n; doubl ...

  5. sqoop操作之HDFS导出到ORACLE

    注意:在导出前需要先创建待导出的表结构.如果导出的表在数据库中不存在则会报错:如果重复导出多次,表中的数据会重复: ; ; 导出表的所有字段 sqoop export --connect jdbc:o ...

  6. python3 string

    字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可.例如: var1 = 'Hello World!' var2 ...

  7. nginx, supervisor

    Nginx(单进程): 反向代理, 负载均衡.图解 将配置文件 nginx.conf 的 user xx 配置好 xx用户 检查语法 $ sudo service nginx configtest 重 ...

  8. IOUtils总结

    参考:https://www.cnblogs.com/xing901022/p/5978989.html 常用的静态变量 在IOUtils中还是有很多常用的一些变量的,比如换行符等等 public s ...

  9. web常见攻击

    DoS和DDoS攻击 DoS(Denial of Service),即拒绝服务,造成远程服务器拒绝服务的行为被称为DoS攻击.其目的是使计算机或网络无法提供正常的服务.最常见的DoS攻击有计算机网络带 ...

  10. as2 针对加载进来的swf操作

    如果加载的子swf,里面的东西会随时发生变化,那么as2获取的子swf宽高也会不停在变动. 解决方法,就是在子swf里面控制设置方法,当as2需要准确获取子swf宽高时,迫使子宽高恢复到原本样子,这样 ...