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. ...
随机推荐
- systick运用
systick的原理前一篇博文有介绍,简而言之就是SysTick定时器是一个24位的倒计数,当倒计数为0时,将从RELOAD寄存器中取值作为定时器的初始值,同时可以选择在这个时候产生中断(异常号:15 ...
- 记忆化搜索:POJ1579-Function Run Fun(最基础的记忆化搜索)
Function Run Fun Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14815 Accepted: 7659 Des ...
- SpringMVC基本概念
DispatcherServlet:MVC的前端控制器,浏览器用户的请求经过DispatcherServlet的分发,到达合适的controller,生产业务数据所需要的model,model通过Di ...
- python-os模块及md5加密
常用内置方法 __doc__打印注释 __package__打印所在包 __cached__打印字节码 __name__当前为主模块是__name__ == __main__ __file__打印文件 ...
- Windows命令行中pip install jieba,但没有安装到anaconda3中
系统混淆了python3环境下的pip和anaconda3环境下的pip. 找到Anaconda3的Scripts目录,我这里是C:\Users\Diane\Anaconda3\Scripts 将该目 ...
- table中填写数据并批量增加
<table class = "table jtable table-bordered table-striped hide" id = "table_1" ...
- Mysql InnoDB事务
http://www.cnblogs.com/benshan/archive/2013/01/19/2867244.html 事务的四个特性 1.原子性(atomicity)原子性是指整个数据库事务是 ...
- POJ 3678 Katu Puzzle(2-SAT,合取范式大集合)
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9987 Accepted: 3741 Descr ...
- [NOI2008][bzoj1061] 志愿者招募 [费用流+巧妙的建图]
题面 传送门 思路 引入:网络流? 看到这道题,第一想法是用一个dp来完成决策 但是,显然这道题的数据并不允许我们进行dp,尤其是有10000种志愿者的情况下 那么我们就要想别的办法来解决: 贪心?这 ...
- bzoj 4131: 并行博弈 (parallel)
bzoj 4131: 并行博弈 (parallel) Description lyp和ld在一个n*m的棋盘上玩翻转棋,游戏棋盘坐标假设为(x, y),1 ≤ x ≤ n,1 ≤ y ≤ m,这个游戏 ...