Leetcode solution 124: Binary Tree Maximum Path Sum
Problem Statement
Given a non-empty binary tree, find the maximum path sum.
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.
Example 1:
Input: [1,2,3]
1
/ \
2 3
Output: 6
Example 2:
Input: [-10,9,20,null,null,15,7] -10
/ \
9 20
/ \
15 7 Output: 42
Problem link
Video Tutorial
You can find the detailed video tutorial here
Thought Process
When dealing with binary tree related problem, traversals using recursion is our friend. It seems we can perform a post-order traversal, and keep track of the maximum sums.
If the path has to go through root, then in each post-order step, we will have the max_sum_of_the_left_path, max_sum_of_the_right_path, the current_node_value, we simply return and record
single_path_max = max(the current_node_value, max(max_sum_of_the_left_path, max_sum_of_the_right_path) + current_node_value)
However, the problem allows a path that not goes through the root, therefore, we need to also record a max between left + current node value + right, i.e.,
global_max = max(single_path_max, max_sum_of_the_left_path + current_node_value + max_sum_of_the_right_path)
One caveat is in your recursion, we should still return the single_path_max. The reason we should not return the global_max is in that case, it will not be a single node to single node path.

Solutions
Post-order recursion
private int max = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
maxPathSumHelper(root);
return this.max;
}
public int maxPathSumHelper(TreeNode root) {
if (root == null) {
return 0;
}
int left = maxPathSumHelper(root.left);
int right = maxPathSumHelper(root.right);
// the max on a single path
int singlePath = Math.max(root.val, Math.max(left, right) + root.val);
// max across the current node on two sides
int acrossPath = Math.max(singlePath, left + right + root.val);
if (acrossPath > this.max) {
this.max = acrossPath;
}
// Note: always want to return the single path for recursion, because you cannot include both path, else,
// it will not be a path
return singlePath;
}
Time Complexity: O(N), each node is visited once
Space Complexity:No extra space is needed other than the recursion function stack
References
Leetcode solution 124: Binary Tree Maximum Path Sum的更多相关文章
- 【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 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 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 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- [LeetCode] 124. 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 ...
- leetcode@ [124] Binary Tree Maximum Path Sum (DFS)
https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ...
- [leetcode]124. 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 ...
- LeetCode 124. Binary Tree Maximum Path Sum 二叉树中的最大路径和 (C++/Java)
题目: Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as ...
- leetcode 124. Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
随机推荐
- 微信小程序生成随机数
const charts = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K', ...
- Oracle 学习笔记二
一.oracle通用函数vnl(a,b) 用于任何类型,如果a的值不为null返回a的值否则返回b的值 条件判断oracle中可以使用 case 字段 when 条件1 then 表达式1 when ...
- 洛谷 P1970 花匠
题目描述 花匠栋栋种了一排花,每株花都有自己的高度.花儿越长越大,也越来越挤.栋栋决定把这排中的一部分花移走,将剩下的留在原地,使得剩下的花能有空间长大,同时,栋栋希望剩下的花排列得比较别致. 具体而 ...
- bzoj2431 || 洛谷P1521 求逆序对
考虑一下插⼊法 n<=100n<=100n<=100 f[i][j]f[i][j]f[i][j]表⽰111~iii的全排列有j个逆序对的⽅案数 f[i][j]=Σf[i−1][j−k ...
- HTTPS协议学习笔记
在前一段时间准备面试的时候,面试官反复提到了HTTPS这个协议.我只是单纯的知道,HTTPS是安全的应用层协议 是HTTP更安全的版本,通过对称密钥加密.但是具体的其他的,可能我不太了解.今天就专门抽 ...
- kali换源
在/tec/apt/sources.list加入以下内容 #中科大更新源 deb https://mirrors.ustc.edu.cn/kali kali-rolling main non-free ...
- [sublime3] 在linux下的终端中使用sublime3打开文件
通过ln命令创建软连接实现 echo $PATH 查看路径 例 我的路径是: /home/rh/anaconda3/bin:/home/rh/bin:/home/rh/.local/bin:/usr/ ...
- python检测变量名
python检测变量名 变量在编程中的用途非常广,在python中,变量的名称只能以字母或者下划线“_”开头,变量名只能由字母.数字.下划线组成. 编写python,使得其实现以下功能: 1.输入一个 ...
- iView学习笔记(一):Table基本操作(包含前后端示例代码)
iView表格操作 1.前端准备工作 首先新建一个项目,然后引入iView插件,配置好router npm安装iView npm install iview --save cnpm install i ...
- jango简介
Django简介 Django框架简介 MVC框架和MTV框架 MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Mode ...