LeetCode OJ-- Binary Tree Maximum Path Sum ***
https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/
给一棵二叉树,路径可以从任一点起,到任一点结束,但是可以连成一个路径的。求路径上最大和。
一般来说,路径是 V 字状的, 递归解的话, 每个点记录它的 左右子树的最大 V 值,并且和其的 V值相比较, 选最大的那个返回。
同时,为了计算它自己的 V 值,需要保留着,它左子树的 最大 path和,右子树的最大 path 和,这个path指的是 直的,没拐弯的。
注意,要对路径的值为负数的时候的处理。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxPathSum(TreeNode *root) {
if(root == NULL)
return ; int ans = ;
calcV(root, ans);
return ans;
}
// return data is the biggest path
int calcV(TreeNode* root, int ¤t_biggest_v)
{
if(root == NULL)
{
current_biggest_v = ;
return ;
}
if(root->left == NULL && root->right == NULL)
{
current_biggest_v = root->val;
return root->val;
}
int left_biggest_v;
int right_biggest_v; int left_path = calcV(root->left, left_biggest_v);
int right_path = calcV(root->right, right_biggest_v); int ret_val = ; ret_val = max(left_path, right_path);
ret_val = max(ret_val, );
ret_val = ret_val + root->val; // with ? statements, with should add bracks
int current_v = (left_path>? left_path:) +( right_path>? right_path:) + root->val; if(root->left && root->right == NULL)
current_biggest_v = max(left_biggest_v, current_v);
else if(root->right && root->left == NULL)
current_biggest_v = max(right_biggest_v, current_v);
else
{
current_biggest_v = max(left_biggest_v, right_biggest_v);
current_biggest_v = max(current_biggest_v, current_v);
} return ret_val;
}
};
LeetCode OJ-- Binary Tree Maximum Path Sum ***的更多相关文章
- 【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 ...
- 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 (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 ...
- 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. ...
- [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 ...
- 【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. ...
随机推荐
- CSS-标准盒模型 & 怪异盒模型
CSS中Box model分类 CSS中Box model是分为两种:: W3C标准 和 IE标准盒子模型. 大多数浏览器采用W3C标准模型,而IE中则采用Microsoft自己的标准. 怪异模式是“ ...
- Python中的bytes
bytes_lst = [ ('创建bytes',), ('bytes可哈希',), ('编码与解码',), ('常见编码类型',), ('ord() 与 chr()',), ] 创建bytes &g ...
- python编写定时执行脚本
前几天在抓博客园文章,打算每天抓10条最新的,所以就在脚本中加了定时让它在每天凌晨四点中时执行,但是昨天发现,报错了: 显示是远程主机强制关闭了一个链接, 原因是:mysql数据库默认当连续8小时不对 ...
- 678. Valid Parenthesis String
https://leetcode.com/problems/valid-parenthesis-string/description/ 这个题的难点在增加了*,*可能是(也可能是).是(的前提是:右边 ...
- [BZOJ3684][拉格朗日反演+多项式求幂]大朋友和多叉树
题面 Description 我们的大朋友很喜欢计算机科学,而且尤其喜欢多叉树.对于一棵带有正整数点权的有根多叉树,如果它满足这样的性质,我们的大朋友就会将其称作神犇的:点权为\(1\)的结点是叶子结 ...
- 命令java 找不到或无法加载主类
这个是由于用了package导致cmd下找不到class文件产生的错误,解决方案: 方法1.删除HelloWord.java源程序中的第一行package demo1:然后在cmd下正常使用javac ...
- pyautogui 模块学习
在日常实施中,我们用控件对大部分的网页和客户端都能进行拾取操作.但是仍有一小部分的应用无法进行操作.这里我常用到 pyautogui 这个模块.下面做个分享. Python 的 pyautogui 模 ...
- 2 Model层 - 模型查询
1.简介 查询集表示从数据库中获取的对象集合 查询集可以含有零个.一个或多个过滤器 过滤器基于所给的参数限制查询的结果 从Sql的角度,查询集和select语句等价,过滤器像where和limit子句 ...
- 如何排查Java内存泄漏?看完我给跪了!
没有经验的程序员经常认为Java的自动垃圾回收完全使他们免于担心内存管理.这是一个常见的误解:虽然垃圾收集器做得很好,但即使是最好的程序员也完全有可能成为严重破坏内存泄漏的牺牲品.让我解释一下. 当不 ...
- 【3Sum Closest 】cpp
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...