【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 to go through the root.

For example:
Given the below binary tree,

       1
      /      2   3

Return 6.

题解:

  感觉解题思路有点类似于动态规划,一个变量用于表示以此节点为根的最大路径和,另一个变量为全局变量,全局更新最大值

 1 class Solution {
 2 public:
 3     int maxPathSum(TreeNode* root) {
 4         if (!root)
 5             return 0;
 6         int res = INT_MIN;
 7         helper(root, res);
 8         return res;
 9     }
10
11     int helper(TreeNode* root, int& res) {
12         if (!root)
13             return 0;
14
15         int lmax = max(helper(root->left, res), 0);
16         int rmax = max(helper(root->right, res), 0);
17         res = max(lmax + rmax + root->val, res);
18         return max(lmax, rmax) + root->val;
19     }
20 };

原文地址:https://www.cnblogs.com/Atanisi/p/8832131.html

时间: 2024-08-28 03:53:06

【LeetCode】124. Binary Tree Maximum Path Sum的相关文章

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 leas

【Lintcode】094.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. Example Given the below binary tree: 1 / 2 3 return 6. 题解: Solution 1 ()  from here class Solution { public: int maxPathSum(TreeNode *root) { if (

第四周 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