题目描述

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. 通过Long类型的出生日期算年龄

    package com.utils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.u ...

  2. MTK 使用iptable 命令来完成网络路由(android WIFI/4G分享网络)

    很多时候,总有些奇怪的需求,这时候是发挥我们的聪明才智的时候!! 有客户的需求是: 公网 WIFI 4G/其他网络 以太网1 以太网2 内部设备 描述下需求:现今有一个控制设备,里面有WIFI,物联网 ...

  3. python 识别图片上的数字

    https://blog.csdn.net/qq_31446377/article/details/81708006 ython 3.6 版本 Pytesseract 图像验证码识别 环境: (1) ...

  4. P4177 [CEOI2008]order(网络流)最大权闭合子图

    P4177 [CEOI2008]order 如果不能租机器,这就是最大权闭合子图的题: 给定每个点的$val$,并给出限制条件:如果取点$x$,那么必须取$y_1,y_2,y_3......$,满足$ ...

  5. Bugku-CTF之welcome to bugkuctf(php://filter和php://input的妙用)

    Day24 welcome to bugkuctf http://123.206.87.240:8006/test1/ 本题要点:代码审计,PHP://filter ,  php://input ,  ...

  6. iOS 与 Swift 方法互相调用

    1.OC调用swift 在OC项目中创建一个swift文件时,Xcode会提示 需要创建一个桥接文件,点击确定创建桥接文件,Xcode会自动创建一个桥接文件,名字:工程名-Bridging-Heade ...

  7. export ,export default 和 import 区别 以及用法

    首先要知道export,import ,export default是什么 ES6模块主要有两个功能:export和importexport用于对外输出本模块(一个文件可以理解为一个模块)变量的接口i ...

  8. Java基础之数组(一)

    数组的定义 数组是相同类型数据的有序集合.数组描述的是相同类型的若干个数据,按照 一定的先后次序排列组合而成.其中,每一个数据称作一个元素,每个元素可以通过一个索引(下标)来访问它们.数组的三个基本特 ...

  9. loadrunner常用函数集锦

    一.三个复制函数的区别: strcpy 原型:extern char *strcpy(char *dest,char *src);用法:#i nclude功能:把src所指由NULL结束的字符串复制到 ...

  10. php list()函数

    说明: (PHP 4, PHP 5, PHP 7) list — 把数组中的值赋给一组变量 像 array() 一样,这不是真正的函数,而是语言结构. list() 可以在单次操作内就为一组变量赋值. ...