二叉树中的最大路径和 · Binary Tree Maximum Path Sum
[抄题]:
给出一棵二叉树,寻找一条路径使其路径和最大,路径可以在任一节点中开始和结束(路径和为两个节点之间所在路径上的节点权值之和)
[思维问题]:
不会写分合法
[一句话思路]:
用两次分治:root2any any2any分一次,左右再分一次。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
先root-any左右各一次,再用any-any。
[一刷]:
- left right都是resultType类型,要用到helper函数
- root2Any any2Any都不是helper中的变量,需要重新定义:左边或右边的any2any, 递归才是加上中间的any2any
[二刷]:
- root为空的corner case中,any2any并不是0,而是MIN_VALUE,保证其它任何数都比它大。
- 没有理解递归的实质:a = left.a,一定要出现相同的变量才行
- 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的更多相关文章
- [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 ...
- 二叉树最大路径和-Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum
题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...
- 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 ...
- [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 ...
- 【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 ...
- 【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 ...
- 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- 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 ...
随机推荐
- MariaDB管理系统
MariaDB管理系统 [root@c4kaichen@163 ~]# yum install mariadb[root@c4kaichen@163 ~]# yum install -y mariad ...
- java的super和this关键字用法总结
------super关键字------ super用途:在子类中访问超类“被隐藏的成员变量(无论是否静态)和静态方法”以及“被重写的实例方法”.这里的超类必须是“直接 ...
- Erasure Coding(纠删码)深入分析
http://blog.sina.com.cn/s/blog_57f61b490102viq9.html 1.前言 Swift升级到2.0大版本后宣称开始支持纠删码,这其实是一个很有意义的特性,主要是 ...
- 基于Linux的Samba开源共享解决方案测试(二)
单NAS网关50Mb码率视音频文件的稳定读测试结果如下: 50Mb/s负载性能记录 NAS网关资源占用 稳定读 稳定读 CPU空闲 内存空闲 网卡占用 13个稳定流 96.70% 10G 104MB/ ...
- for /f命令之—Delims和Tokens用法&总结
在For命令语踞饽参数F中,最难理解的就是Delims和Tokens两个选项,本文简单的做一个比较和总拮.“For /f”常用来解析文本,读取字符串.分工上,delims负责切分字符串,而tokens ...
- jQuery实现todo及轮播图
内容: 1.todo程序 2.轮播图 1.todo程序 需求: 实现一个todo程序,可以添加数据,可以删除数据,可以修改数据,可以查看所有数据 另外实现自己的一系列弹窗:用于提示用户的提示框.用于警 ...
- 汇编环境配置及 Hello World。DOSBox,debug.exe,VisualStudio
▶ DOSBOX 相关 ● 下载 DOSBox(http://www.dosbox.com/download.php?main=1),安装到文件夹 DOSBox . ● 下载 debug.exe(Wi ...
- 19.OGNL与ValueStack(VS)-OGNL入门
转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 下面我们在com.asm.vo.User类中增加一个字段private Ad ...
- Linux&Unix命令
Linux下: 系统操作 文件操作 防火墙 权限管理 压缩和解压 安装应用 用户管理 端口 PID 应用 start & shutdown 远程操作 异常 注意点: linux系统下内容大多用 ...
- Linux下类似windows下_beginthread和_endthread 的多线程开发
在 windows下头文件中包含 #include<process.h> 就可以使用_beginthread进行线程创建.个人感觉挺方便的. 在linux下类似于_beginthread ...