http://www.geeksforgeeks.org/how-to-determine-if-a-binary-tree-is-balanced/

 #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; struct node {
int data;
struct node *left, *right;
node() : data(), left(NULL), right(NULL) { }
node(int d) : data(d), left(NULL), right(NULL) { }
}; void print(node *node) {
if (!node) return;
print(node->left);
cout << node->data << " ";
print(node->right);
} bool balanced(node *root, int &height) {
if (!root) return true;
int l = ;
int r = ;
if (!balanced(root->left, l) || !balanced(root->right, r)) return false;
if (l - r >= || r - l >= ) return false;
height = max(l, r) + ;
return true;
} int main() {
struct node* root = new node();
root->left = new node();
root->right = new node();
root->left->left = new node();
root->left->right = new node();
root->right->left = new node();
root->left->left->left = new node();
int height = ;
if (balanced(root, height)) cout << "yes" << endl;
else cout << "NO" << endl;
return ;
}

Data Structure Binary Tree: How to determine if a binary tree is height-balanced?的更多相关文章

  1. [Data Structure] 二叉搜索树(Binary Search Tree) - 笔记

    1. 二叉搜索树,可以用作字典,或者优先队列. 2. 根节点 root 是树结构里面唯一一个其父节点为空的节点. 3. 二叉树搜索树的属性: 假设 x 是二叉搜索树的一个节点.如果 y 是 x 左子树 ...

  2. [Data Structure] Tree - relative

    Segment Tree First, try to build the segment tree. lintcode suggest code: Currently recursion recomm ...

  3. Python: tree data structure

    # 树结构 from pythonds.basic.stack import Stack #pip install pythonds from pythonds.trees.binaryTree im ...

  4. [Algorithms] Tree Data Structure in JavaScript

    In a tree, nodes have a single parent node and may have many children nodes. They never have more th ...

  5. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  6. LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design

    字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith  ...

  7. CDOJ 483 Data Structure Problem DFS

    Data Structure Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/proble ...

  8. [转]Data Structure Recovery using PIN and PyGraphviz

    Source:http://v0ids3curity.blogspot.com/2015/04/data-structure-recovery-using-pin-and.html --------- ...

  9. [Algorithm] Trie data structure

    For example we have an array of words: [car, done, try, cat, trie, do] What is the best data structu ...

随机推荐

  1. linux路由服务

    本文介绍怎样使用linux创建一台简单的路由server. 主要包含几个參数的设置:ip_forward和rp_filter. 1.开启IP forwarding # 重新启动后失效 $ echo & ...

  2. SQL数据库从高版本到低版本的迁移,同时解决sql脚本文件太大无法打开的尴尬问题

    as we known,sql数据库高版本向低版本还原是不太可能但是又经常会碰到的事,今天实测了一种方法 步骤:任务—>生成脚本—> 下一步->高级,选择数据库版本和编写脚本数据类型 ...

  3. 无需Root实现Android手机屏幕流畅投影到电脑进行演示(附软件下载)

    近期要在公司的会议上演示App,须要在投影仪上显示出来给大家演示.因为投影仪不是智能投影仪,仅仅能将App先投影到自己的笔记本上.然后再将笔记本上的内容投影到投影仪上.该App是个游戏,实时交互性比較 ...

  4. python常见面试题(一)

    1.Python是如何进行内存管理的? 答:从三个方面来说,一对象的引用计数机制,二垃圾回收机制,三内存池机制 一.对象的引用计数机制 Python内部使用引用计数,来保持追踪内存中的对象,所有对象都 ...

  5. 过年啦!小B高兴的不行了,她收到了很多红包,可以实现好多的愿望呢。小B可是对商店货架上心仪的货物红眼好久了,只因囊中羞涩作罢,这次她可是要大大的shopping一番。小B想去购物时,总是习惯性的把要买的东西列在一个购买清单上,每个物品单独列一行(即便要买多个某种物品),这次也不例外。

    include "stdafx.h" #include<iostream> #include<vector> #include <algorithm& ...

  6. wordcloud使用

    学了下怎么用wordcloud. 以imet的数据集为例 https://www.kaggle.com/c/imet-2019-fgvc6 读取“train.csv”,”label.csv”文件,得到 ...

  7. Lumen开发:lumen源码解读之初始化(3)——单例(singleton)与中间件(Middleware)

    版权声明:本文为博主原创文章,未经博主允许不得转载. 今天来讲讲Lumen的singleton和Middleware,先来看看起始文件bootstrap/app.php / * | --------- ...

  8. centos7.0 activemq的安装

    1:下载地址http://activemq.apache.org/activemq-590-release.html 2:wget http://archive.apache.org/dist/act ...

  9. Bag of mice(概率DP)

    Bag of mice  CodeForces - 148D The dragon and the princess are arguing about what to do on the New Y ...

  10. java拾遗1----XML解析(一) DOM解析

    XML解析技术主要有三种: (1)DOM(Document Object Model)文档对象模型:是 W3C 组织推荐的解析XML 的一种方式,即官方的XML解析技术. (2)SAX(Simple ...