Cracking The Coding Interview 4.1
- //Implement a function to check if a tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that no two leaf nodes differ in distance from the root by more than one.
- //
- // 译文:
- //
- // 实现一个函数检查一棵树是否平衡。对于这个问题而言, 平衡指的是这棵树任意两个叶子结点到根结点的距离之差不大于1。
- #include <iostream>
- #include <string>
- using namespace std;
- class tree
- {
- public:
- tree()
- {
- //root = create();
- root = NULL;
- index = 0;
- }
- /***输入扩展层次遍历序列,#表示该节点为空***/
- tree(char *s)
- {
- root = NULL;
- index = 0;
- if (s == NULL)
- {
- return;
- }
- int size = strlen(s);
- create(&root,s,0,size);
- }
- ~tree()
- {
- /***清空二叉树***/
- }
- /***二叉排序树:插入。***/
- void insert(char *s)
- {
- if (s == NULL)
- {
- return;
- }
- int size = strlen(s);
- for (int i = 0; i<size; i++)
- {
- insert(&root, s[i]);
- }
- }
- bool isBanlance(int size)
- {
- int *s = new int[size];
- depth(root,s,size,0);
- s[index+1] = '\0';
- int i = 0;
- int max = s[0];
- int min = s[0];
- while(s[i]!='\0')
- {
- if (s[i]>max)
- {
- max = s[i];
- }
- if (s[i]<min)
- {
- min = s[i];
- }
- i++;
- }
- return ((max - min)>1? 0:1);
- }
- void preOrder(){ pOrder(root);}
- void inOreder(){ zOrder(root);}
- void postOreder(){ hOrder(root);}
- private:
- struct treenode
- {
- char data;
- treenode * left;
- treenode * right;
- };
- treenode *root;
- int index;
- void insert(treenode **p, char s)
- {
- if (((*p) == NULL) && s != '\0')
- {
- *p = new treenode;
- (*p)->data = s;
- (*p)->left = NULL;
- (*p)->right = NULL;
- }
- else
- {
- if ((*p)->data > s)
- {
- insert(&((*p)->left) , s);
- }
- else
- {
- insert(&((*p)->right) , s);
- }
- }
- }
- void depth(treenode *s, int *str,int size,int k)
- {
- if (s==NULL)
- {
- return;
- }
- if (s->left == NULL && s->right == NULL)
- {
- str[index]= k;
- index++;
- }
- else
- {
- depth(s->left,str, size, k+1);
- depth(s->right,str, size, k+1);
- }
- }
- treenode* create()
- {
- treenode *p;
- char t;
- cout<<"请输入:"<<endl;
- t = getchar();
- if (t=='#')
- {
- p = NULL;
- }
- else
- {
- p = new treenode;
- p->data = t;
- cout<<"create tree node: "<<t<<endl;
- p->left = create();
- p->right = create();
- }
- return p;
- }
- void create(treenode **p, char *str, int i, int size)
- {
- if (i>size-1 || str[i] == '\0')
- {
- *p = NULL;
- return;
- }
- if (str[i] == '#')
- {
- *p=NULL;
- }
- else
- {
- *p = new treenode;
- (*p)->data = str[i];
- create(&((*p)->left),str,2*i+1,size);
- create(&((*p)->right),str,2*i+2,size);
- }
- }
- void pOrder(treenode *p)
- {
- if (p==NULL)
- {
- return;
- }
- cout<<p->data<<" "<<endl;
- pOrder(p->left);
- pOrder(p->right);
- }
- void zOrder(treenode *p)
- {
- if (p==NULL)
- {
- return;
- }
- zOrder(p->left);
- cout<<p->data<<" "<<endl;
- zOrder(p->right);
- }
- void hOrder(treenode *p)
- {
- if (p==NULL)
- {
- return;
- }
- hOrder(p->left);
- cout<<p->data<<" "<<endl;
- hOrder(p->right);
- }
- };
- int main()
- {
- /***扩展层次序列简立树***/
- //char t[9] = "ABCDE#FG";
- //tree s(t);
- //s.preOrder();
- /***建立二叉排序树***/
- tree s;
- char t[8] = "3289654";
- s.insert(t);
- // s.postOreder();
- cout<<"is Banlance? "<<s.isBanlance(8)<<endl;
- cout<<"Over"<<endl;
- return 0;
- }
Cracking The Coding Interview 4.1的更多相关文章
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)
#include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...
随机推荐
- 数据结构(C语言版)-第2章 线性表
#define MAXSIZE 100 //最大长度 typedef struct { ElemType *elem; //指向数据元素的基地址 int length; //线性表的当前长度 }SqL ...
- [转] @JoinColumn 详解 (javax.persistence.JoinColumn)
原文链接:@JoinColumn详解 原文标的也是转载,但是没有注明原文链接,看起来乱乱的,所以整理一下转载过来,顺便细看一下 1. 一对一 现假设有Person表和Address表,是一对一的关系 ...
- Run-time code to create charts:
tChart1.Series.Clear(); tChart1.Series.Add(new Steema.TeeChart.Styles.Bar());tChart1.Series[0].Clear ...
- 雷林鹏分享:jQuery EasyUI 窗口 - 自定义窗口工具栏
jQuery EasyUI 窗口 - 自定义窗口工具栏 默认情况下,窗口(window)有四个工具:collapsible.minimizable.maximizable 和 closable.比如我 ...
- You Don't Know JS: Async & Performance(第2章,Callbacks)
Chapter 2: Callbacks. Callbacks are by far the most common way that asynchrony in JS programs is exp ...
- P3721 [AH2017/HNOI2017]单旋
题目:https://www.luogu.org/problemnew/show/P3721 手玩一下即可AC此题. 结论:插入x后,x要么会成为x的前驱的右儿子,要么成为x的后继的左儿子,这取决于它 ...
- python-flask-路由匹配源码分析
@app.route('/') def hello_world(): return 'Hello World!' 第1步: class Flask(_PackageBoundObject): def ...
- C# 3.0 / C# 3.5 Lambda 表达式
概述 Lambda 表达式的本质就是匿名函数.(而匿名方法的本质是委托) “Lambda 表达式”是一个匿名函数,可以包含表达式和语句,并且可用于创建委托或表达式树类型. (Lambda 表达式的运算 ...
- 使用AdminLTE 在content区,打开相应网页
参考:https://bbs.csdn.net/topics/391846671 问: 比如打开starter.html,然后点击其左边栏的链接(如user.html)的时候,怎么实现在右边的cont ...
- MySql习题和答案
MySQL测试题 一.表关系请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号.ps:针对的是自己的生物成绩比物理成绩高,再把符合 ...