【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 end at any node in the tree.

For example:
Given the below binary tree,

       1
      /      2   3

Return 6.

用递归确定每一个节点作为root时,从root出发的最长的路径

在每一次递归中计算maxPath

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     int maxSum=INT_MIN;
13     int maxPathSum(TreeNode *root) {
14
15         DFS(root);
16         return maxSum;
17     }
18
19     int DFS(TreeNode *root)
20     {
21         if(root==NULL)
22         {
23             return 0;
24         }
25
26         int left=DFS(root->left);
27         int right=DFS(root->right);
28
29         int sum=root->val;
30         if(left>0) sum+=left;
31         if(right>0) sum+=right;
32         if(maxSum<sum) maxSum=sum;
33
34         return (left>0||right>0)?root->val+max(left,right):root->val;
35     }
36 };
时间: 2024-10-01 05:04:11

【leetcode】Binary Tree Maximum Path Sum的相关文章

【LeetCode】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. [解析] 题意:在二叉树中找一条路径,使得该路径的和最大.该路径可以从二叉树任何结点开始,也可以到任何结点结束. 思路:递归求一条经过root的最大路径,这条路径可能是:

【leetcode】Binary Tree Maximum Path Sum (medium)

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. 找树的最大路径和 注意路径可以从任意点起始和结束. 我发现我真的还挺擅长树的题目的,递归不难.就是因为有个需要比较的量(最大和),所以需要再写一个函数. 因为路径可以从任意点起始和结束,所以每次递归的时候左右子树小于等于0的就可以不管了. #include <iostream> #include

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

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

Leetcode OJ: Binary Tree Maximum Path Sum

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. 这个问题可以转化为,以某节点作为祖先,并经过这一节点的和最大的路径. 于就其实就是要遍历每一个节点,并对此节点求出 1. 以

[LeetCode][Java] 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. 题意: 给定一棵二叉树,找出最大得路径和. 路径的起始和结束位置可以是树中的任意节点. 比如, 给定如下的一棵二叉树 1 / 2 3 返回  6. 算法分析: 1) Rec

8.10 [LeetCode] 173 Binary Tree Maximum Path Sum

Qestion 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, (the node.val may < 0) 1 / 2 3 / \ 4 5 Return 11. Analysis get leftMax, rightMax, then compare the ro

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

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". "右子树的根节点

LeetCode – Refresh – Binary Tree Maximum Path Sum

Use lMax get the maximum value of a sub SINGLE branch (left branch or right branch or just current node). Use gMax get the maximum value of the whole sub branch (include current node). So gMax need to pass by reference. 1 /** 2 * Definition for binar