这道题是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. windows服务器安装安全狗时服务名如何填写

    安全狗安装时“服务名”这一栏指的是apache进程的服务名称,即进入“任务管理-服务”里显示的名称. phpstudy等软件搭建的环境需要设置运行模式为“系统服务”后才能看到服务名.

  2. Elasticsearch-基本操作1

    Elasticsearch版本:6.0 一.文档 一个文档不仅包含数据,也包含元数据,三个必须的元数据如下 _index:具有共同特性分到一起的文档集合,标示了文档的存放位置: 名字小写,不以下划线开 ...

  3. LeetCode:103Binary Tree Zigzag Level Order Traversal

    真是不容易啊,做这道题的时候脑子一团乱,感觉还是得劳逸结合啊.这道题的思想不难,就是宽搜BFS.通过设置一个flag来判断是否需要逆序输出. 我的做法虽然AC,但是觉得代码还是不好,空间占用较多. / ...

  4. CentOS-语言设置

    查看所有的locale语言 # locale -a # locale -a|grep en 查看当前操作系统使用的语言 # echo $LANG 设置系统locale语言为中文环境(永久生效) # v ...

  5. UVA 11925 Generating Permutations 生成排列 (序列)

    题意:要用一个有序的序列生成给定序列,操作有两种,一是交换前两个元素,二是把第一个元素移动到最后去. 思路有两种: 1.映射,把给定序列映射成有序的序列,然后按照同样的替换规则把有序的序列映射掉,然后 ...

  6. netbackup如何手动获取主机ID证书。

    如何手动获取主机ID证书.   文章:100039650 最后发布:2017-09-21 评分:  20 11 产品:NetBackup 问题 从NetBackup V8.1开始,管理员需要在证书颁发 ...

  7. JQuery EasyUI学习记录(一)

    1.主页设计(JQuery EasyUI插件) 下载easyUI开发包: 将easyUI资源文件导入页面中: <link rel="stylesheet" type=&quo ...

  8. oracle中print_table存储过程实例介绍

    oracle中pro_print_table存储过程实例介绍 存储过程(Stored Procedure),就是一组用于完成特定数据库功能的SQL语句集,该SQL语句集经过编译后存储在数据库系统中.这 ...

  9. 头文件string与string.h的区别

    在C++中,#include<iostream>与#include<iostream.h>的区别,前者要使用更新的编译器(其实大部分编译器多比较前卫了,出了有些搞嵌入式的用变态 ...

  10. 转 Hystrix入门指南 Introduction

    https://www.cnblogs.com/gaoyanqing/p/7470085.html