题目描述

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. React Native之常用组件(View)

    一. JSX和组件的概念 React的核心机制之一就是虚拟DOM:可以在内存中创建的虚拟DOM元素.React利用虚拟DOM来减少对实际DOM的操作从而提升性能.传统的创建方式是: 但这样的代码可读性 ...

  2. hiho一下 第207周

    题目1 : The Lastest Time 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 What is latest time you can make with ...

  3. Python VisibleDeprecationWarning: converting an array with ndim > 0 to an index will result in an error in the future

    问题原因:nd.array值直接用做数组索引,索引一般需为整数,可能带来风险,比如浮点数作为索引 解决方案:把nd.array值强制转成int peakIdx = int( np.asarray(pe ...

  4. Swagger和Postman的配置和使用

    Swagger 1. 配置 pom文件添加swagger依赖,注意版本,2.8.0可以使用 <dependency> <groupId>io.springfox</gro ...

  5. Deep Learning--week1~week3

    week1 一张图片,设像素为64*64, 颜色通道为红蓝绿三通道,则对应3个64*64实数矩阵 为了用向量表示这些矩阵,将这些矩阵的像素值展开为一个向量x作为算法的输入 从红色到绿色再到蓝色,依次按 ...

  6. 20175312 2018-2019-2 《Java程序设计》第8周学习总结

    20175312 2018-2019-2 <Java程序设计>第8周学习总结 教材学习内容总结 已依照蓝墨云班课的要求完成了第十章的学习,主要的学习渠道是PPT,和书的课后习题. 总结如下 ...

  7. js添加的元素无法触发click事件

    动态生成的元素,使用.on绑定事件,比如$(document).on("click",".divclick",function(){})

  8. C语言--第4次作业

    1.本章学习总结 1.1思维导图 1.2本章学习体会及代码量学习体会 1.2.1学习体会 初识数组:这几周第一次接触数组,感觉有点懵,是一个很陌生的知识点,但是运用范围及其广泛,大大简化了程序,增大了 ...

  9. 近期wxss总结

    最近有一些需要点击才能实现的样式切换,我用的方法有2种 1 wxml 中 对class给一个判断式 class="变量?变化后的类:变化前的类" 这样在js中设一个变量,我是设成布 ...

  10. 改变选择文字的color及background-color

    在一些特殊的网站中,常常会有着一些新奇的体验,在阅读网页的时候相信许多人都会和我一样有着一个习惯,把一些文字选中然后进行阅读,或者时要复制粘贴的时候选择文字对吧.然而无论是在ie,chrome,fir ...