二叉树中的最大路径和 · 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 ...
随机推荐
- post 中文数据到elasticsearch restful接口报json_parse_exception 问题
我们的客户端程序直接调用es 的restful接口, 通过post json数据去查询, 但post数据有中文的时候,有些中文会报异常,有些中文不会 {"error":{" ...
- 转 - ubuntu 安装node.js 与 npm
原文链接为: https://blog.csdn.net/wangtaoking1/article/details/78005038 这篇文章介绍如何在ubuntu环境下安装node环境. 我使用的系 ...
- hive的查询注意事项以及优化总结 .
Hive是将符合SQL语法的字符串解析生成可以在Hadoop上执行的MapReduce的工具.使用Hive尽量按照分布式计算的一些特点来设计sql,和传统关系型数据库有区别, 所以需要去掉原有关系型数 ...
- 对 Spring 的核心(AOP 和 IOC)的理解(大白话)
Spring 首先它是一个开源而轻量级的框架.其核心容器的主要组件是Bean工厂(BeanFactory).Bean工厂使用控制反转(IOC)模式来降低程序代码之间的耦合度,并提供了面向切面编程(AO ...
- OpenCL 设备队列
▶ 按书上写的设备队列的代码,需要 OpenCL2.0 的平台和设备,先把代码堆上来 ● 程序主要功能:用主机上的数组 Ahost 和 Bhost 创建设备缓冲区 Adevice 和 Bdevice, ...
- Latex Error:‘acmart.cls’ not found 解决方案:
windows下latex编译ACM论文模板时,出现Latex Error:‘acmart.cls’ not found,解决方案: 首先cd至模板所在目录下,然后运行以下命令: tex acma ...
- php获取服务器信息类
<?php/**+------------------------------------------------------------------------------* 获取服务器信 ...
- Struts2:No result defined for action com.yibai.user.action.LoginAction and result input
转自:https://zhidao.baidu.com/question/133574016.html 1 String 里面有5个static 常量分别是: ERROR INPUT LOGIN NO ...
- java ee7 -- Java Bean验证
针对对象.对象成员.方法.构造函数的数据验证. 1. 一个验证的小例子 (1) 添加引用jar <dependency> <groupId>org.hibernate.vali ...
- ESCP打印机数据解密
通过串口调试工具 抓取到的16进制文本; 如下 然后打开我们的文档,查看命令数据内容. 详情请密我QQ:1161588342 说明加好友原因