二叉树中的最大路径和 · 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 ...
随机推荐
- Linux 期中架构 SSH
为其他网络服务提供安全协议.替代Telnet SSH: 实现数据加密传输 22 默认支持root用户远程连接 类似sftp-server服务 nmap -p 22 10.0.0.41 nc 10 ...
- [UE4]动画序列面板
- pyH支持python3
记录下,感谢大神,原地址https://www.cnblogs.com/yunmenzhe/p/6293428.html,侵删 1.修改xxx/python3.5/pyh.py权限 sudo chmo ...
- PostgreSQL下安装pg_stat_statements
一.安装过程如下:进入postgreSQL安装包的contrib/pg_stat_statements目录,执行编译和安装动作:用root用户 make && make install ...
- ASP.NET基于Redis的Provider库
因为session基于本地cache,以前我们自己写分布式缓存,或者数据库存储,或者cookie加密存储,来保存用户状态信息,但较少的直接通过创建一个继承 SessionStateStoreProvi ...
- CSS3 盒阴影(box-shadow)详解
CSS3 的 box-shadow 有点类似于 text-shadow,只不过不同的是 text-shadow 是对象的文本设置阴影,而 box-shadow 是给对象实现图层阴影效果.本文我们搁下I ...
- Mybatis通过colliection属性递归获取菜单树
1.现有商品分类数据表category结构如下,三个字段都为varchar类型 2.创建商品分类对应的数据Bean /** * */ package com.xdw.dao; import java. ...
- 经典算法 BFPRT算法详解
内容: 1.原始问题 => O(N*logN) 2.BFPRT算法 => O(N) 1.原始问题 问题描述:给你一个整型数组,返回其中第K小的数 普通解法: 这道题可以利用 ...
- uva-10344
题意: 枚举23点,注意,数字也是可以枚举的,wa了一次 #include<stdio.h> #include<iostream> #include<sstream> ...
- spring boot 自定义异常
1.创建一个异常: public class LdapQueryException extends Exception { private Integer code; private String m ...