这道题是LeetCode里的第110道题。

题目要求:

给定一个二叉树,判断它是否是高度平衡的二叉树。

本题中,一棵高度平衡二叉树定义为:

一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。

示例 1:

给定二叉树 [3,9,20,null,null,15,7]

    3
/ \
9 20
/ \
15 7

返回 true 。

示例 2:

给定二叉树 [1,2,2,3,3,null,null,4,4]

       1
/ \
2 2
/ \
3 3
/ \
4 4

返回 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:
bool isBalanced(TreeNode* root) {
if(root==NULL)return true;
int l_len=Depth(root->left);
int r_len=Depth(root->right);
if(abs(l_len-r_len)<=1&&
isBalanced(root->left)&&
isBalanced(root->right))
return true;
return false;
}
int Depth(TreeNode* root){
if(root==NULL)return 0;
return max(Depth(root->left),Depth(root->right))+1;
}
};

提交结果:

个人总结:

这道题用迭代法似乎挺难的还没具体的思路,如果树的深度越深,则开销就越大,要处理的左右子树也很多。

【LeetCode】Balanced Binary Tree(平衡二叉树)的更多相关文章

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

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

  2. [Leetcode] Balanced binary tree平衡二叉树

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

  3. [CareerCup] 4.1 Balanced Binary Tree 平衡二叉树

    4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a ...

  4. LeetCode: Balanced Binary Tree 解题报告

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

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

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

  6. LeetCode——Balanced Binary Tree(判断是否平衡二叉树)

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

  7. LeetCode之Balanced Binary Tree 平衡二叉树

    判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree ...

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

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

  9. [leetcode]Balanced Binary Tree @ Python

    原题地址:http://oj.leetcode.com/problems/balanced-binary-tree/ 题意:判断一颗二叉树是否是平衡二叉树. 解题思路:在这道题里,平衡二叉树的定义是二 ...

随机推荐

  1. filter和map的使 使得数组对象变数组

    let UnitList = this.paytypeData.filter( item => item.CheckBox === true ).map(axis => axis.Unit ...

  2. Android开发学习——小细节注意

    Android中通过Intent调用其他应用的方法(转) Android中两种序列化方式的比较Serializable和Parcelable http://www.jcodecraeer.com/a/ ...

  3. 看动画,秒懂人工智能&物联网

  4. 首次将项目从svn下载到eclipse

    1.点击 File --> Import,进入导入项目窗口 2.选择从SVN检出项目,点击Next 3.选择创建新的资源库位置,点击Next 4.在URL处输入SVN项目远程地址,点击Next ...

  5. JavaSe-算数运算符

    算数运算符包括:+.-.*./.%.++.-- 代码: package com.java.chap02; public class Demo07 { public static void main(S ...

  6. 【Python图像特征的音乐序列生成】关于数据库到底在哪里下载

    毕竟原网站一个是14年前的一个是16年前的…… 1,http://ifdo.ca/~seymour/nottingham/nottingham.html 这个网站可以下载zip包. 2,https:/ ...

  7. 随记:UWP开发中怎么使当前页面拓展到标题栏

    public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); CoreAp ...

  8. 远程桌面连接(mstsc)无法断开的解决方案

    某2008 r2有二个系统账户,同时允许两个tcp连接,但每个用户仅同时允许一人登录.不知何因,关闭远程桌面连接的窗口后,再次连接就瞬间提示无法被连接.查看了事件查看器,信息为被注销,这是因为设置了“ ...

  9. 菜鸟的数据库实战-4-数据阅读器SqlDataReader

    老铁们大家好啊,我是菜鸟思奎,今天我学习的是数据库和前端的连接用到的字符串,如果有什么纰漏希望大家在评论区指正.阿里嘎多. 我的环境是Visual Studio 2008 + Microsoft SQ ...

  10. UVALive 3026 Period (KMP算法简介)

    kmp的代码很短,但是不太容易理解,还是先说明一下这个算法过程吧. 朴素的字符串匹配大家都懂,但是效率不高,原因在哪里? 匹配过程没有充分利用已经匹配好的模版的信息,比如说, i是文本串当前字符的下标 ...