Data Structure Binary Tree: How to determine if a binary tree is height-balanced?
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?的更多相关文章
- [Data Structure] 二叉搜索树(Binary Search Tree) - 笔记
1. 二叉搜索树,可以用作字典,或者优先队列. 2. 根节点 root 是树结构里面唯一一个其父节点为空的节点. 3. 二叉树搜索树的属性: 假设 x 是二叉搜索树的一个节点.如果 y 是 x 左子树 ...
- [Data Structure] Tree - relative
Segment Tree First, try to build the segment tree. lintcode suggest code: Currently recursion recomm ...
- Python: tree data structure
# 树结构 from pythonds.basic.stack import Stack #pip install pythonds from pythonds.trees.binaryTree im ...
- [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 ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- 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 ...
- CDOJ 483 Data Structure Problem DFS
Data Structure Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/proble ...
- [转]Data Structure Recovery using PIN and PyGraphviz
Source:http://v0ids3curity.blogspot.com/2015/04/data-structure-recovery-using-pin-and.html --------- ...
- [Algorithm] Trie data structure
For example we have an array of words: [car, done, try, cat, trie, do] What is the best data structu ...
随机推荐
- JUnit单元测试中的setUpBeforeClass()、tearDownAfterClass()、setUp()、tearDown()方法小结
编写JUnit单元测试的时候,会用到 setUpBeforeClass().tearDownAfterClass().setUp().tearDown()这四个方法,例如用 eclipse新建一个ju ...
- Android实用工具
1 json类:hiJson 格式化json字符串 2 sqlite类:sqlitespy,SQLiteExpertSetup 3
- 2010年imac从移动硬盘启动Win10
虽然是个程序员,但也爱折腾. 原WIN10不想折腾,虚拟机折腾大点的软件太卡,不能完全发挥硬件水平. 原材料(硬件):2010年imac一台,80G移动硬盘一块(个人组装,硬盘盒+3.5寸IDE硬盘) ...
- vim 窗口管理
一.切分窗口 1. 水平切分 命令::sp file 快捷键:<Ctrl-W> + S 2. 垂直切分 命令: :vsp file 快捷键:<Ctrl-W> + V 二.关 ...
- Html.BeginForm 与Section、Partial View 和 Child Action
该方法用于构建一个From表单的开始,他的构造方法为: Html.BeginForm("ActionName","ControllerName",FormMet ...
- .net 哈希表和字典的基本用法
哈希表 传送门:https://www.cnblogs.com/xpvincent/archive/2013/01/15/2860841.html using System; using System ...
- webuploader插件使用中的一点东西
本人绝对菜鸟,高手勿喷 菜鸟开发中的解决方法,高手勿喷 1.针对同一应用中不同的类别,存放不同的路径 在页面中添加,hidden属性的标记,如: type="hidden" ...
- linux网络及防火墙配置命令
/etc/sysconfig/network 包括主机基本网络信息,用于系统启动 /etc/sysconfig/network-script/ 此目录下是系统启动最初始化网络的信息 /etc/sy ...
- Oracle 技术支持之现场优化的思维路径
性能调优是每个DBA职业生涯中都能遇到的任务 大到世界五百强的核心系统,小到乡镇企业的进销存,几乎都会有要调优的时候 面对形形色色的系统,林林总总的需求,调优的手段也是丰富多彩 定位问 ...
- Gmail收不到邮件咋办?
http://www.ipip.net/ping.php 分别输入 imap.gmail.com pop.gmail.com smtp.gmail.com 选择 国外 , 然后点ping,找到对应的 ...