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 example:
Given the below binary tree,

       1
      /      2   3

Return 6.

求二叉树的最大路径和,感觉好复杂,但是分析一下,由于中间的点不能重叠,所以说肯定一部分在某个点的左边一个在右边。所以可以遍历左右子树来求最大值,相当于遍历所有的单点以及其左右子树之后,不断的更新最大值,用ret全局变量来维护这个最大值。将总体当作根节点加上左边和右边就可以了,代码如下:

 1 class Solution {
 2 public:
 3     int maxPathSum(TreeNode *root) {
 4         if(!root) return ret;
 5         ret = INT_MIN;
 6         dfs(root);
 7         return ret;
 8     }
 9
10     int dfs(TreeNode * node)
11     {
12         if(node == NULL) return 0;
13         int val = node->val;
14         int left = dfs(node->left);
15         int right = dfs(node->right);
16         if(left > 0) val += left;
17         if(right > 0) val += right;
18         if(val > ret)
19             ret = val;
20         return max(node->val, max(node->val + left, node->val + right));
21     }
22 private:
23     int ret;
24 };
时间: 2024-10-14 06:05:48

LeetCode OJ:Binary Tree Maximum Path Sum(二叉树最大路径和)的相关文章

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 OJ - Binary Tree Maximum Path

这道题需要注意的地方有以下一些: 1. 求从子树中的某节点到当前节点的最大路径不能采用递归方法,因为这个部分会被反复的调用,如果用递归,会使得之前已经计算过的节点被重复计算,使得时间复杂度特别高: 2. 树中有节点的值是负数的. 下面是AC代码.(我发现AC并不代表代码真的完全正确!!) 1 /** 2 * Given a binary tree, find the maximum path sum. 3 * The path may start and end at any node in t

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

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

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

[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. 题目意思很简单,就是给定一棵二叉树,求最大路径和.path 可以从任意 node 开始,到任意 node 结束. 这道题在 LeetCode 上的通过率只有 20% 多一点,并被标记

[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

【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的最大路径,这条路径可能是:

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

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