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

 1 private int max = Integer.MIN_VALUE;
 2
 3 public int maxPathSum(TreeNode root) {
 4     maxPathSumHelper(root);
 5     return this.max;
 6 }
 7
 8 public int maxPathSumHelper(TreeNode root) {
 9     if (root == null) {
10         return 0;
11     }
12
13     int left = maxPathSumHelper(root.left);
14     int right = maxPathSumHelper(root.right);
15
16     // the max on a single path
17     int singlePath = Math.max(root.val, Math.max(left, right) + root.val);
18     // max across the current node on two sides
19     int acrossPath = Math.max(singlePath, left + right + root.val);
20     if (acrossPath > this.max) {
21         this.max = acrossPath;
22     }
23
24     // Note: always want to return the single path for recursion, because you cannot include both path, else,
25     // it will not be a path
26     return singlePath;
27 }

Time Complexity: O(N), each node is visited once

Space Complexity:No extra space is needed other than the recursion function stack

References

原文地址:https://www.cnblogs.com/baozitraining/p/11404552.html

时间: 2024-10-14 04:51:40

Leetcode solution 124: Binary Tree Maximum Path Sum的相关文章

【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 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

第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)

124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节点为结尾(包含它或者不包含)的最大值,有两种情况,分别来自左儿子和右儿子设为Vnow. 然后考虑经过这个节点的情况来更新最终答案.更新答案后返回Vnow供父节点继续更新. 代码很简单. 有一个类似的很有趣的题目,给定一个二叉树,选择一条路径,使得权值最大的和最小的相差最大.参考POJ3728 cla

Java for LeetCode 124 Binary Tree Maximum Path Sum

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. 解题思路: DFS暴力枚举,注意,如果采用static 全局变量的话,在IDE里面是可以通过,但在OJ上无法测试通过,因此需要建立一个类来储存结果,JAVA实现如下: public

[email protected] [124] Binary Tree Maximum Path Sum (DFS)

https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a 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 connection

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 of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root. For exampl

LeetCode OJ:Binary Tree Maximum Path Sum(二叉树最大路径和)

Given a 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 does not need to go through the root. For exampl

124. Binary Tree Maximum Path Sum (Tree; DFS)

Given a 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 does not need to go through the root. For exampl

124. Binary Tree Maximum Path Sum *HARD* -- 二叉树中节点和最大的路径的节点和

Given a 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 does not need to go through the root. For exampl

Leetcode #124 Binary Tree Maximum Path Sum

题目链接:https://leetcode.com/problems/binary-tree-maximum-path-sum/ 题中要求 maxPathSum(TreeNode *root) 函数返回二叉树中最大的 "Path Sum". 而这个最大的 "Path Sum" 可以被分解为三部分之和: "左子树的根节点" 到 "左子树的某个节点" 的 最大 "Path Sum". "右子树的根节点