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.
  2. //
  3. // 译文:
  4. //
  5. // 实现一个函数检查一棵树是否平衡。对于这个问题而言, 平衡指的是这棵树任意两个叶子结点到根结点的距离之差不大于1。
  6.  
  7. #include <iostream>
  8. #include <string>
  9. using namespace std;
  10.  
  11. class tree
  12. {
  13. public:
  14.  
  15. tree()
  16. {
  17. //root = create();
  18. root = NULL;
  19. index = 0;
  20. }
  21.  
  22. /***输入扩展层次遍历序列,#表示该节点为空***/
  23. tree(char *s)
  24. {
  25. root = NULL;
  26. index = 0;
  27. if (s == NULL)
  28. {
  29. return;
  30. }
  31.  
  32. int size = strlen(s);
  33. create(&root,s,0,size);
  34. }
  35.  
  36. ~tree()
  37. {
  38. /***清空二叉树***/
  39. }
  40.  
  41. /***二叉排序树:插入。***/
  42. void insert(char *s)
  43. {
  44. if (s == NULL)
  45. {
  46. return;
  47. }
  48.  
  49. int size = strlen(s);
  50. for (int i = 0; i<size; i++)
  51. {
  52. insert(&root, s[i]);
  53. }
  54. }
  55.  
  56. bool isBanlance(int size)
  57. {
  58. int *s = new int[size];
  59. depth(root,s,size,0);
  60. s[index+1] = '\0';
  61. int i = 0;
  62. int max = s[0];
  63. int min = s[0];
  64. while(s[i]!='\0')
  65. {
  66. if (s[i]>max)
  67. {
  68. max = s[i];
  69. }
  70. if (s[i]<min)
  71. {
  72. min = s[i];
  73. }
  74. i++;
  75. }
  76. return ((max - min)>1? 0:1);
  77. }
  78.  
  79. void preOrder(){ pOrder(root);}
  80. void inOreder(){ zOrder(root);}
  81. void postOreder(){ hOrder(root);}
  82.  
  83. private:
  84. struct treenode
  85. {
  86. char data;
  87. treenode * left;
  88. treenode * right;
  89. };
  90.  
  91. treenode *root;
  92. int index;
  93.  
  94. void insert(treenode **p, char s)
  95. {
  96. if (((*p) == NULL) && s != '\0')
  97. {
  98. *p = new treenode;
  99. (*p)->data = s;
  100. (*p)->left = NULL;
  101. (*p)->right = NULL;
  102. }
  103. else
  104. {
  105. if ((*p)->data > s)
  106. {
  107. insert(&((*p)->left) , s);
  108. }
  109. else
  110. {
  111. insert(&((*p)->right) , s);
  112. }
  113. }
  114. }
  115.  
  116. void depth(treenode *s, int *str,int size,int k)
  117. {
  118. if (s==NULL)
  119. {
  120. return;
  121. }
  122. if (s->left == NULL && s->right == NULL)
  123. {
  124. str[index]= k;
  125. index++;
  126. }
  127. else
  128. {
  129. depth(s->left,str, size, k+1);
  130. depth(s->right,str, size, k+1);
  131. }
  132. }
  133.  
  134. treenode* create()
  135. {
  136. treenode *p;
  137. char t;
  138. cout<<"请输入:"<<endl;
  139. t = getchar();
  140. if (t=='#')
  141. {
  142. p = NULL;
  143. }
  144. else
  145. {
  146. p = new treenode;
  147. p->data = t;
  148. cout<<"create tree node: "<<t<<endl;
  149. p->left = create();
  150. p->right = create();
  151.  
  152. }
  153. return p;
  154. }
  155.  
  156. void create(treenode **p, char *str, int i, int size)
  157. {
  158.  
  159. if (i>size-1 || str[i] == '\0')
  160. {
  161. *p = NULL;
  162. return;
  163. }
  164.  
  165. if (str[i] == '#')
  166. {
  167. *p=NULL;
  168. }
  169. else
  170. {
  171. *p = new treenode;
  172. (*p)->data = str[i];
  173. create(&((*p)->left),str,2*i+1,size);
  174. create(&((*p)->right),str,2*i+2,size);
  175. }
  176. }
  177.  
  178. void pOrder(treenode *p)
  179. {
  180. if (p==NULL)
  181. {
  182. return;
  183. }
  184.  
  185. cout<<p->data<<" "<<endl;
  186. pOrder(p->left);
  187. pOrder(p->right);
  188. }
  189.  
  190. void zOrder(treenode *p)
  191. {
  192. if (p==NULL)
  193. {
  194. return;
  195. }
  196. zOrder(p->left);
  197. cout<<p->data<<" "<<endl;
  198. zOrder(p->right);
  199. }
  200.  
  201. void hOrder(treenode *p)
  202. {
  203. if (p==NULL)
  204. {
  205. return;
  206. }
  207. hOrder(p->left);
  208. cout<<p->data<<" "<<endl;
  209. hOrder(p->right);
  210. }
  211. };
  212.  
  213. int main()
  214. {
  215. /***扩展层次序列简立树***/
  216. //char t[9] = "ABCDE#FG";
  217. //tree s(t);
  218. //s.preOrder();
  219.  
  220. /***建立二叉排序树***/
  221. tree s;
  222. char t[8] = "3289654";
  223. s.insert(t);
  224. // s.postOreder();
  225.  
  226. cout<<"is Banlance? "<<s.isBanlance(8)<<endl;
  227. cout<<"Over"<<endl;
  228.  
  229. return 0;
  230. }

Cracking The Coding Interview 4.1的更多相关文章

  1. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  2. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  3. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  4. 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 ...

  5. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  6. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  7. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

  8. 《Cracking the Coding Interview》——第5章:位操作——题目7

    2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...

  9. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  10. 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)

    #include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...

随机推荐

  1. 数据结构(C语言版)-第2章 线性表

    #define MAXSIZE 100 //最大长度 typedef struct { ElemType *elem; //指向数据元素的基地址 int length; //线性表的当前长度 }SqL ...

  2. [转] @JoinColumn 详解 (javax.persistence.JoinColumn)

    原文链接:@JoinColumn详解  原文标的也是转载,但是没有注明原文链接,看起来乱乱的,所以整理一下转载过来,顺便细看一下 1. 一对一 现假设有Person表和Address表,是一对一的关系 ...

  3. Run-time code to create charts:

    tChart1.Series.Clear(); tChart1.Series.Add(new Steema.TeeChart.Styles.Bar());tChart1.Series[0].Clear ...

  4. 雷林鹏分享:jQuery EasyUI 窗口 - 自定义窗口工具栏

    jQuery EasyUI 窗口 - 自定义窗口工具栏 默认情况下,窗口(window)有四个工具:collapsible.minimizable.maximizable 和 closable.比如我 ...

  5. 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 ...

  6. P3721 [AH2017/HNOI2017]单旋

    题目:https://www.luogu.org/problemnew/show/P3721 手玩一下即可AC此题. 结论:插入x后,x要么会成为x的前驱的右儿子,要么成为x的后继的左儿子,这取决于它 ...

  7. python-flask-路由匹配源码分析

    @app.route('/') def hello_world(): return 'Hello World!' 第1步: class Flask(_PackageBoundObject): def ...

  8. C# 3.0 / C# 3.5 Lambda 表达式

    概述 Lambda 表达式的本质就是匿名函数.(而匿名方法的本质是委托) “Lambda 表达式”是一个匿名函数,可以包含表达式和语句,并且可用于创建委托或表达式树类型. (Lambda 表达式的运算 ...

  9. 使用AdminLTE 在content区,打开相应网页

    参考:https://bbs.csdn.net/topics/391846671 问: 比如打开starter.html,然后点击其左边栏的链接(如user.html)的时候,怎么实现在右边的cont ...

  10. MySql习题和答案

    MySQL测试题 一.表关系请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号.ps:针对的是自己的生物成绩比物理成绩高,再把符合 ...