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(直径)的更多相关文章

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

    124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...

  2. 【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 ...

  3. [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 ...

  4. 【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 ...

  5. 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 ...

  6. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

    题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...

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

  8. [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 ...

  9. [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 ...

随机推荐

  1. Flask在Pycharm开启调试模式

       一.Flask在Pycharm2018前的版本只需设置(两种方法之一): 1. 直接设置app的debug为true: app.debug=true 2. 把debug=true作为参数,传入到 ...

  2. Spring中四种实例化bean的方式

    本文主要介绍四种实例化bean的方式(注入方式) 或者叫依赖对象实例化的四种方式.上面的程序,创建bean 对象,用的是什么方法 ,用的是构造函数的方式 (Spring 可以在构造函数私有化的情况下把 ...

  3. canvas-2arcTo.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 【读书笔记】iOS-访问iPod媒体库

    iOS设备内部都有一个iPod媒体库,在这个媒体库中包含了音频和视频文件,它的来源主要是苹果公司的iTunes Store,现在苹果公司的iTunes Store内容很多,但能够下载到iPod媒体库的 ...

  5. 机器学习算法(SVM)公开课4月25日开讲

    从深蓝到AlphaGo,聪明的人工智能一再“羞辱”人类大脑: 指纹识别.以图搜图.语音助手.无人驾驶···生活里它无孔不入 离不开智能手机的我们,是否已开始被人工智能的“奴役”? 或许,你不需要会运用 ...

  6. Angular基础(三) TypeScript

    一.模仿Reddit a) 运行ng new –ng4angular-reddit创建应用,从随书代码中复制样式文件,新建组件app-root,代码为: 界面可以看到了: b) 对于界面输入的数据,获 ...

  7. Kotlin入门(15)独门秘笈之特殊类

    上一篇文章介绍了Kotlin的几种开放性修饰符,以及如何从基类派生出子类,其中提到了被abstract修饰的抽象类.除了与Java共有的抽象类,Kotlin还新增了好几种特殊类,这些特殊类分别适应不同 ...

  8. spring cloud 配置文件application.yml和bootstrap.yml 的定位,区别和联系总算是有一点明白了

    最近在启用springcloud配置中心server的东西,在整理属性资源的时候,突然发现:用了这么久的springboot,为什么会配置两个属性文件同时存在(application.yml/prop ...

  9. 机器学习实战(Machine Learning in Action)学习笔记————10.奇异值分解(SVD)原理、基于协同过滤的推荐引擎、数据降维

    关键字:SVD.奇异值分解.降维.基于协同过滤的推荐引擎作者:米仓山下时间:2018-11-3机器学习实战(Machine Learning in Action,@author: Peter Harr ...

  10. [20171223]grid用户的环境变量问题.txt

    [20171223]grid用户的环境变量问题.txt --//oracle 11G 安装RAC,一般需要建立grid用户,使用这个用户管理asm,群集信息.--//在安装过程中,同事的疑问实际上也是 ...