Leetcode:110. 平衡二叉树

Leetcode:110. 平衡二叉树

点链接就能看到原题啦~

关于AVL的判断函数写法,请跳转:平衡二叉树的判断

废话不说直接上代码吧~主要的解析的都在上面的链接里了

自顶向下写法

/**
* 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 getHeight(TreeNode* root){
if(root==NULL) return 0;
return max(getHeight(root->right),getHeight(root->left))+1;
}
bool isBalanced(TreeNode* root) {
if(root==NULL) return true;
if(isBalanced(root->left)&&isBalanced(root->right))
if(abs(getHeight(root->left)-getHeight(root->right))<2)
return true;
return false;
}
};

自底向上写法

/**
* 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 isBalancedHelper(TreeNode* root,int& height){
if(root==NULL){
height=0;
return true;
}
int left,right;
if(isBalancedHelper(root->right,right)&&isBalancedHelper(root->left,left)&&abs(left-right)<2){
height=max(left,right)+1;
return true;
}
return false;
}
bool isBalanced(TreeNode* root) {
int height=0;
return isBalancedHelper(root,height);
}
};

Leetcode:110. 平衡二叉树的更多相关文章

  1. LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15

    110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...

  2. Java实现 LeetCode 110 平衡二叉树

    110. 平衡二叉树 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9 ...

  3. LeetCode 110.平衡二叉树(C++)

    给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9,20,null,nu ...

  4. [LeetCode 110] - 平衡二叉树 (Balanced Binary Tree)

    问题 给出一棵二叉树,判断它是否在高度上是平衡的. 对于本问题,高度上平衡的二叉树定义为:每个节点的两棵子树的深度差永远不大于1的一棵二叉树. 初始思路 根据定义,思路应该比较直接:递归计算每个节点左 ...

  5. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  6. LeetCode:平衡二叉树【110】

    LeetCode:平衡二叉树[110] 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 ...

  7. [LeetCode] 110. Balanced Binary Tree 平衡二叉树

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  8. LeetCode 110. Balanced Binary Tree (平衡二叉树)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  9. 递归 - Leetcode 110 判断二叉树是否为平衡二叉树

    110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...

随机推荐

  1. 真机调试报The executable was signed with invalid entitlements.错误

    真机运行时,提示The executable was signed with invalid entitlements.(The entitlements specified in your appl ...

  2. [ PyQt入门教程 ] PyQt5中多线程模块QThread使用方法

    本文主要讲解使用多线程模块QThread解决PyQt界面程序唉执行耗时操作时,程序卡顿出现的无响应以及界面输出无法实时显示的问题.用户使用工具过程中出现这些问题时会误以为程序出错,从而把程序关闭.这样 ...

  3. 从DirectX SDK升级到Windows SDK

    原来的DirectX SDK到June 2010,微软就不更新了.之后新的版本被集成到了Windows SDK中. 在微软的博客里找到一篇升级指南:http://blogs.msdn.com/b/ch ...

  4. 个人第4次作业——alpha项目测试

    这个作业属于哪个课程 http://edu.cnblogs.com/campus/xnsy/GeographicInformationScience 这个作业的要求在哪里 https://www.cn ...

  5. Isx个人第4次作业—Alpha项目测试

    标题 内容 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience 这个作业要求在哪里 https:// ...

  6. orcle导入大数据文件

    0,创建ctl文件  内容为 OPTIONS (skip=0) LOAD DATA  CHARACTERSET 'UTF8' INFILE 'F:\anhui_data\20180814\shangh ...

  7. Rabbitmq | ConnectionException:Connection refused: connect

    案例 今天完成了Rabbitmq的搭建,调用本地mq服务器是可以的,但是在本地调用远程mq发现出现了connectionException异常,使用的是默认端口5672,具体情况如下图 解决方案 修改 ...

  8. I Love GPLT

    这道超级简单的题目没有任何输入. 你只需要把这句很重要的话 —— “I Love GPLT”——竖着输出就可以了. 所谓“竖着输出”,是指每个字符占一行(包括空格),即每行只能有1个字符和回车. 输入 ...

  9. A Simple Introduction To Computer Networking

    Most networking discussions are a jumble of acronyms. Forget the configuration details - what are th ...

  10. Oracle笔记(1)--emp表查询(1)

    (1)截取函数--TRUNC() 的用法 SELECT  TRUNC(789.652) 截取小数, TRUNC(789.652,2) 截取两位小数, TRUNC(789.652,-2) 取整 FROM ...