Ch4-1: identify if a tree is "balanced" (注:CTCI 第四版和第五版的定义不一样)
来源:互联网 发布:手机直播源码 编辑:程序博客网 时间:2024/05/29 15:54
To identify if the most different leaves, it is: If < 1, then balance. otherwise, not.
So we need to traverse the tree to find the leaves' depth, so we pick 1 of 3 traverse oders: the inorder.
In order to implement a tree, need a structure:
struct BinNode{int data;BinNode *parent, *lChild, *rChild;};
Then we can traverse each node and find the depth of each leaf. Read this snippet, very elegant recursion version (although not high efficient as iteration version since large constant coefficient of O(n)).
Look at the: ++d, dep[++numLeaf]=d, --d;
// the helper methodint d=0, numLeaf=0, dep[MAX_NUM];void getDep(BinNode* head){if(head == NULL) return;++d; // key in recursiongetDep(head->lChild);if(head->lChild==NULL && head->rChild==NULL){dep[++numLeaf] = d; //}getDep(head->rChild);--d; // key in recursion}
Then we can simply check all the dep[numLeaf] and find the largest, smallest one, and find the difference of them.
Also, as the linked list, we need to implement tree, so we need to initialize it by init() and insert().
The insert() is elegant too.
// very good design of the insert method!void insert(BinNode* &head, int value){if(head==NULL) {//head->data = value; Hawstein used array to store BSTBST[idx].data = value;BST[idx].parent = p;head = &BST[idx++]; // note, this is post increment return; // C++ primer P223, this return equal to break};p = head;if(value < head->data)insert(head->lChild, value);elseinsert(head->rChild, value);}
And here is the whole code:
and the output:
Executing the program....$demo 0testing num_a 15: BST[0]3: BST[1]8: BST[2]1: BST[3]4: BST[4]7: BST[5]10: BST[6]2: BST[7]6: BST[8]9: BST[9]11: BST[10]12: BST[11]testing num_a 2data: 100x6018a0
0 0
- Ch4-1: identify if a tree is "balanced" (注:CTCI 第四版和第五版的定义不一样)
- Q4.1 Implement a function to check if a tree is balanced
- Ch4.3: transform an ordered array into a balanced Tree
- LeetCode题目笔记(四) -- Check if a binary tree is balanced
- check a tree is balanced or not
- ch4.7: given 2 huge Binary tree t1, t2(>1millon nodes), decide if one tree is subtree of another one
- Ch4.4: array to Balanced tree and print each level
- Binary Tree Operations(IV) - Determine if a Binary Tree is a Binary Search Tree
- Check if a given Binary Tree is SumTree
- Game Programming Gems 1中关于has a 和 is a的定义
- NYOJ129 树的判定 || POJ1308 Is It A Tree? 【并查集应用,树的定义】
- poj 1308 &&HDU 1325 Is It A Tree?(并查集、树的定义)
- 【leetCode】 Balanced Binary Tree python版实现
- Check if a binary tree is subtree of another binary tree
- Check if tree b is part of tree a JAVA 实现
- 13022:PMBOK第四版和第五版变更对比汇总
- Database 中 identify relationship 和 non-identify relationship的区别
- A program to check if a binary tree is BST or not
- poj2362
- 安装GCC编译器
- 机器学习课程成绩单
- centos 无线网卡配置
- echo 命令
- Ch4-1: identify if a tree is "balanced" (注:CTCI 第四版和第五版的定义不一样)
- 各种桑心哇
- scanf的用法
- 算法导论 2-1思考题 合并排序中对小数组采用插入排序
- windows 8.1使用Android模拟器并用intel HAXM加速导致蓝屏
- 黑马程序员_Date
- 成熟男人的标准 你达到几条
- 认识JavaScript中的作用域和上下文
- Maximum Subarray