这道题是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. Python+selenium之带unittest的脚本分析

    from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.c ...

  2. dataset datatable datacolums datarow

    DataSet 表示数据在内存中的缓存. 属性 Tables  获取包含在 DataSet 中的表的集合. ds.Tables["sjxx"] DataTable 表示内存中数据的 ...

  3. UWP开发:应用设置存储

    应用设置储存指的是保存在应用程序储存区中的键/值对的字典集合,它自动负责序列化对象,并将其保存在应用程序里.以键/值对方式提供一种快速数据访问的方式,主要用于储存一些应用信息. 1,简介 应用设置是W ...

  4. UVA 10954 Add All 全部相加 (Huffman编码)

    题意:给你n个数的集合,每次选两个删除,把它们的和放回集合,直到集合的数只剩下一个,每次操作的开销是那两个数的和,求最小开销. Huffman编码.Huffman编码对于着一颗二叉树,这里的数对应着单 ...

  5. CentOS 软RAID5

    yum install -y mdadm np1回车 tfdw mdadm --create /dev/md5 --level=5 --raid-devices=3 /dev/sdb1 /dev/sd ...

  6. 跑edgebox

    这是edge的作者的代码:https://github.com/pdollar/edges 这是matlab写的,还需要装Matlab Image Processing Toolbox和Piotr's ...

  7. Python语言编写脚本时,对日期控件的处理方式

    对日期控件,日期控件的输入控一般是不能手动输入的:把readonly属性去掉就好 其实很简单,我们不去搞时间日期空间,我们把它当成一个普通的input框处理就好了! 但是,很多此类型input框都是禁 ...

  8. -安装与配置 FTP 服务器

    我们经常会使用 FTP,把本地电脑上的文件上传到服务器上,或者把服务器上的文件下载到自己的电脑里面.FTP 有服务端和客户端,FTP 的服务端提供了这种传输文件的服务,FTP 的客户端提供了传输文件的 ...

  9. jquery Syntax error, unrecognized expression:的解决方法

    原文地址 https://blog.csdn.net/flowingfog/article/details/42739773 问题: 将模板的html内容转换成jquery时报以下错误:Syntax ...

  10. NSOperation、NSOperationQueue

    NSOperation.NSOperationQueue NSOperation 和 NSOperationQueue 配合使用也能实现多线程. NSOperation 继承于 NSObject,是一 ...