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
如果你要计算加上当前节点的最大path和,这个节点的左右子树必定是纯左右树(即没有拐点),
用另一个参数保留整个二叉树的最大path和,然后计算每一个以当前节点为拐点的路径和并且不断更新整个二叉树的最大值
函数的返回值是纯左右子树的最大path,没有拐点
这个题目给root定位为非空,所以直接这样写可以。如果root为空,这样写就会返回INT_MIN这个值,解决办法就是在最开始加是否为空的判断。
class Solution {
public:
int maxPathSum(TreeNode* root) {
int res = INT_MIN;
maxpath(root,res);
return res;
}
int maxpath(TreeNode* root,int &res){
if(root == NULL)
return ;
int left = max(,maxpath(root->left,res));
int right = max(,maxpath(root->right,res));
res = max(res,left + right + root->val);
return max(left,right) + root->val;
}
};
注意:分的过程中,left、right的迭代都使用了同一个res作为参数传入,而自己实现的平衡二叉树使用了两个不同的参数。
首先使用同一个是没有错误的,因为这个题,无论左右子树其实都保存的同一个最大值,无需分别对待。而平衡二叉树中左右子树的深度不一样,使用不同的参数也合理
自己写的一个版本:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxPathSum(TreeNode* root) {
int sum = INT_MIN;
maxPathSum(root,sum);
return sum;
}
int maxPathSum(TreeNode* root,int& sum){
if(!root)
return ;
int sum1 = sum,sum2 = sum;
int left = max(,maxPathSum(root->left,sum1));
int right = max(,maxPathSum(root->right,sum2));
sum = max(left + right + root->val,max(sum1,sum2));
return max(left,right) + root->val;
}
};
必须注意与0比较,因为有负数的情况,可以不加left、right。
必须是INT_MIN,这样可以排除一个负数做根节点,没有左右子树的情况,必须[-3]
这种写法是会超时的,因为你递归了两次;
/**
* Definition for a binary tree node.
* 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)
return -;
int maxSum = INT_MIN;
maxPathSum(root,maxSum);
return maxSum;
}
int maxPathSum(TreeNode* root,int& maxSum){
if(!root)
return ;
int left = maxPathSum(root->left,maxSum) > ? maxPathSum(root->left,maxSum) : ;
int right = maxPathSum(root->right,maxSum) > ? maxPathSum(root->right,maxSum) : ;
maxSum = max(maxSum,left + right + root->val);
return root->val + max(left,right);
}
};
class Solution {
public:
bool isBalanced(TreeNode* root) {
int depth = ;
return Balanced(root,depth);
}
bool Balanced(TreeNode* root,int& depth){
if(root == NULL){
depth = ;
return true;
}
int left,right;
if(Balanced(root->left,left) && Balanced(root->right,right)){
int gap = left - right;
if(gap <= && gap >= -){
depth = left > right ? left + : right + ;
return true;
}
}
return false;
}
};
124题和543题很相似,因为两者实质上都是判断拐点的问题,用返回值表示纯拐点的值,用res值记录最大值就可以。
两者不同的是,124需要加每个节点的值,543是算直径。
543. Diameter of Binary Tree(直径)
https://blog.csdn.net/laputafallen/article/details/80147877
首先明确这里的直径是相当于连接的线的个数,比如4个节点,那直径就是3。所以对于一个节点,他的直径就是他的左右节点的高度和。但是最长的直径不一定是经过根节点的,比如题目中[1,2,3,4,5]这种情况,你如果把4、5都拉长一个,那最长的那个直径对应的根节点应该是2。所以每次求左右子树的高度,然后再跟最大的直径比就好了。
注意:直径是连线的个数,不是节点的个数,比如1、2、3、4连起来,有4个节点,但是实际上只有3条线在连,直径为3,不为4
原始代码:
class Solution {
public:
int diameterOfBinaryTree(TreeNode* root) {
diameterCore(root);
return diameter;
}
int diameterCore(TreeNode* root){
if(root == NULL)
return ;
int left = diameterCore(root->left);
int right = diameterCore(root->right);
diameter = max(diameter,left + right);
return left > right ? left + : right + ;
}
int diameter = ;
};
因为两个题很相似,所以也可以写成124题这样的形式:
class Solution {
public:
int diameterOfBinaryTree(TreeNode* root) {
int res = ;
diameterOfBinaryTree(root,res);
return res;
}
int diameterOfBinaryTree(TreeNode* root,int& res){
if(root == NULL)
return ;
int left = diameterOfBinaryTree(root->left,res);
int right = diameterOfBinaryTree(root->right,res);
res = max(left + right,res);
return max(left,right) + ;
}
};
leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)的更多相关文章
- 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- 【LeetCode】124. 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]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】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: Binary Tree Maximum Path Sum 解题报告
Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and e ...
- 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum
题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...
- 26. 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二叉树最大路径和
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 求二叉树的最大路径和
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
随机推荐
- c# 封装 Request操作类
/// <summary> /// 判断当前页面是否接收到了Post请求 /// </summary> /// <returns>是否接收到了Post请求</ ...
- [android] 手机卫士关闭自动更新
保存数据的四种方式,网络,广播提供者,SharedPreferences,数据库 获取SharedPreferences对象,通过getSharedPreferences()方法,参数:名称,模式 例 ...
- C# Select
- 浮动布局float
浮动 浮动是css里面布局用的最多的属性. .box1{ float: left; width: 300px; ...
- js之选项卡(tag标签)
目标效果:点击不同按钮显示不同内容 代码如下 <!DOCTYPE html> <html lang="en"> <head> <meta ...
- 【读书笔记】iOS-微定位技术
在大型商场,医院或是大楼里,你是否曾经有过找不到想去的地方的经历呢?这种情况下采用传统的定位方法就有些力不从心了.首先这些地方不能采用GPS定们,而Wifi和蜂窝式移动电话基站定位误差比较大.这种情况 ...
- 【读书笔记】iOS-访问网络
iOS平台是按照一直有网络连接的思路来设计的,开发者利用这一特点创造了很多优秀的第三方应用.大多数的iOS应用都需要联网,甚至有些应用严重依赖网络,没有网络就无法正常工作. "在访问网络失败 ...
- Installing Language Tool in TexStudio
This is a recent and more detailed solution for Windows users. Make sure the last version of TeXstud ...
- Android Studio 在项目中引用第三方jar包
在Android Studio项目中引用第三方jar包的方法: 步骤: 1.在build.gradle文件中添加如下代码: 备注:要添加在Android作用域下 sourceSets { main { ...
- Android-滑动解锁高亮文字自定义TextView
public class HightLightTextView extends TextView { // 存储view的宽度 private int mTextViewWidth = 0; // 画 ...