题目描述

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

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofevery node never differ by more than 1.

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int dept(TreeNode *root) {
if(root==NULL) return 0;
if(root->left==NULL && root->right==NULL)
return 1;
return max(dept(root->left),dept(root->right))+1;
} bool isBalanced(TreeNode *root) {
if(root==NULL) return true;
int x=dept(root->left)-dept(root->right);
if(abs(x)>1)
return false;
else
return isBalanced(root->left) && isBalanced(root->right);
}
};

  

balcanced-binary-tree的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  3. Leetcode, construct binary tree from inorder and post order traversal

    Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...

  4. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  5. [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化

    One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...

  6. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  7. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  8. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  9. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  10. [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

随机推荐

  1. FairyGUI TextField

    记录一个在使用FairyGUI的TextField时遇到的坑. TextField有一个文本模板功能,可以实现类似占位符的功能,如:{ number = 0 },然后我们可以在脚本中修改number的 ...

  2. springboot+mybatis环境的坑和sql语句简化技巧

    1.springfox-swagger实体类无限递归 https://hacpai.com/article/1525674135818 里面有不完美的解决方案 不用动源码的解决方案也有,在swagge ...

  3. ssh到虚拟机---一台主机上

    问题描述:我们需要ssh来编辑虚拟机中的文件,以此提高工作效率.但是新建的虚机一般来说没有开启ssh服务,所以需要在虚拟机上开启ssh服务. 1)检查是否安装了SSH rpm -qa |grep ss ...

  4. Android系统修改之Notification布局修改(一)

    源码基于Android4.4 相关布局文件的位置: frameworks/base/core/res目录下: 1. notification_template_base.xml 2. notifica ...

  5. Sony索尼数码录音笔MSV格式转换为MP3格式【转】

    本文转载自:http://blog.sina.com.cn/s/blog_4b2c860f0100d78w.html Sony索尼数码录音笔一般存储为WAV格式,有些没有特意修改存储格式的就保存为MS ...

  6. 快速签发 Let's Encrypt 证书指南

    本文仅记录给自己的网站添加"小绿锁"的动手操作过程,不涉及 HTTPS 工作原理等内容的讲解,感兴趣的同学可以参考篇尾的文章自行了解. 简单了解下我的实验环境: 云服务器:Cent ...

  7. 2019 年 Spread.NET 产品路线图(Roadmap)

    前言 | 问题背景 2018年结束了,12月是Spread.NET的重要月份.我们发布了Spread.NET 12,我们期待着 2019 年令人兴奋的新年,这是Windows Forms开发人员有史以 ...

  8. java.io.IOException: Attempted read from closed stream

    前言: 代码如下,执行的时候提示"java.io.IOException: Attempted read from closed stream." public static JS ...

  9. 剑指offer 09:变态跳台阶

    题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. /* f(n-1) = f(n-2) + f(n-3) + ... + f(0 ...

  10. IBase<T>

    public interface IBase<T>//基类 { IEnumerable<T> SelectAll();//查询所有 T FindById(int ID);//根 ...