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. c# 获取当前绝对路径

    /// <summary> /// 获得当前绝对路径 /// </summary> /// <param name="strPath">指定的路 ...

  2. [PHP]算法-替换空格的PHP实现

    替换空格: 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 思路: 1.先循环一遍,找出 ...

  3. 性能监控(6)–JAVA下的jinfo命令

    jinfo可以用来查看正在运行的java应用程序的扩展参数,设置支持在运行时,修改部分参数. Jinfo的语法为: Usage: jinfo [option] <pid> (to conn ...

  4. Linux Shell脚本编程while语句案例

    1,每隔3秒,打印一次系统负载 #!/bin/bash while true do uptime done 2,把监控结果保存到文件,在后台执行,然后用tail -f监控文件变化 ghostwu@de ...

  5. css文本溢出隐藏显示省略号(单行+多行)

    文本超出若干行就换行,这个功能几乎每个文本浏览网站都会用到,实现它的办法也有很多,今天简单的介绍一下实现它的方法.  一. 单行文本不换行,并将超出文本隐藏 .box-content{     ove ...

  6. OneAPM大讲堂 | Metrics, Tracing 和 Logging 的关系

    [编者按]这是在 OpenTracing 和分布式追踪领域内广受欢迎的一片博客文章.在构建监控系统时,大家往往在这几个名词和方式之间纠结. 通过这篇文章,作者很好的阐述了分布式追踪.统计指标与日志之间 ...

  7. 【PHP调试篇】PHP高性能日志组件SeasLog

    简述 什么是SeasLog SeasLog是一个C语言编写的PHP扩展,提供一组规范标准的功能函数,在PHP项目中方便.规范.高效地写日志,以及快速地读取和查询日志. 为什么使用SeasLog 无论在 ...

  8. Excel快捷键大全 Excel2013/2010/2007/2003常用快捷键大全

    一个软件最大的用处是提高工作效率,衡量一个软件的好坏,除了是否出名之外,最主就是能否让一个新手更快的学会这个软件和提高工作速度.就拿Excel表格来说吧,平常办公中我们经常会用它来制作表格,统计数据或 ...

  9. python第六十五天--python操作mysql

    pymysql模块对mysql进行 import pymysql # 创建连接 conn = pymysql.connect(host='127.0.0.1', port=3306, user='ro ...

  10. [Winform-WebBrowser]-在html页面中js调用winForm类方法

    在winform项目中嵌入了网页,想通过html页面调用后台方法,如何实现呢?其实很简单,主要有三部: 1.在被调用方法类上加上[ComVisible(true)]标签,意思就是当前类可以com组件的 ...