二叉树中的最大路径和 · 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 ...
随机推荐
- mysql存储过程的参数名不要跟字段名一样 (血淋淋的代价)
如题,将会导致的结果就是参数的值将不会是你传入的值,而是变成每条记录的那个字段的值. 这样的后果,是灰常严重的.比如执行删除操作,它能把整个表的记录全删了. 这个是我的血淋淋的代价啊. 死坑如下,勿跳 ...
- C++多线程同步之Mutex(互斥量)
原文链接: http://blog.csdn.net/olansefengye1/article/details/53086141 一.互斥量Mutex同步多线程 1.Win32平台 相关函数和头文件 ...
- Delphi Webbrowser使用方法详解(一)
1.webbroser介绍 该组件是一个浏览器组件,可以显示一个指定地址的网页.设置网页打开时的主页以及对网页进行相关的操作,同时也可以对HTML文件进行剪切.复制.粘贴.删除等操作.该 组件在Int ...
- Python中的LEGB规则
目标 命名空间和作用域——Python从哪里查找变量名? 我们能否同时定义或使用多个对象的变量名? Python查找变量名时是按照什么顺序搜索不同的命名空间? 命名空间与作用域的介绍 命名空间 大约来 ...
- sencha touch在华为emotion ui 2.0自带浏览器中圆角溢出的bug
在华为emotion ui 2.0自带的浏览器中,给部分组件设置了圆角后会发现背景仍然是方的,内部边框是圆的, 对于这种bug, 只需在对应的设置圆角的css样式中加入 background-clip ...
- C# 日志记录工具类--LogHelper.cs测试
C# 日志记录工具类:(适用于不想使用log4j等第三方的Log工具的时候,希望自己写个简单类实现)LogHelper.cs内容如下: using System; using System.Diagn ...
- vconsole h5应用ajax请求抓包
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta co ...
- linux添加计划任务
crond 是linux用来定期执行程序的命令.当安装完成操作系统之后,默认便会启动此任务调度命令.crond命令每分锺会定期检查是否有要执行的工作,如果有要执行的工作便会自动执行该工作.可以用以下的 ...
- JAVA Spring 事物 ( 已转账为例 ) 基于 XML 配置,事务类型说明
< 1 > 配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...
- Astah professional 7.2
分享Astah professional 7.2下载和破解: 官方下载地址:http://astah.net/download 1.免费的community版本 链接:http://pan.baidu ...