二叉树的常见问题及其解决程序(一)

1.二叉树三种周游(traversal)方式:
3.如何判断一棵二叉树是否是平衡二叉树
4.设计一个算法,找出二叉树上任意两个节点的最近共同父结点,复杂度如果是O(n2)则不得分。
5.如何不用递归实现二叉树的前序/后序/中序遍历?
6.在二叉树中找出和为某一值的所有路径(注意是到叶子节点)
7.怎样编写一个程序,把一个有序整数数组放到二叉树中?
8.判断整数序列是不是二叉搜索树的后序遍历结果
9.求二叉树的镜像
10.一棵排序二叉树(即二叉搜索树BST),令 f=(最大值+最小值)/2,设计一个算法,找出距离f值最近、大于f值的结点。复杂度如果是O(n2)则不得分。
11.把二叉搜索树转变成排序的双向链表
12.打印二叉树中的所有路径(与题目6很相似)

解决思路

1.二叉树三种周游(traversal)方式:任何一本数据结构的书都有描述,略过;

2.怎样从顶部开始逐层打印二叉树结点数据

设置一个队列,然后只要队列不为空,将对首元素的左右孩子加入队列(如果左右孩子不为空),然后将队列的首元素出对即可,如下图所示:

二叉树如下图所示:

那么,整个过程如下:

自然,就输出了a,b,c,d,e,f

3.如何判断一个二叉树是否是平衡的?

太简单了,利用递归就可以了:判断根节点的左右子树深度之差是否小于等于1(这里需要用到求深度的方法),如果是,根节点就是平衡的;然后,在判断根节点的左孩子和右孩子是否是平衡的。如此继续下去,直到遇见叶子节点。一旦不是,立刻返回false;

计一个算法,找出二叉树上任意两个节点的最近共同父结点,复杂度如果是O(n2)则不得分

首先找到这两个点key1和key2,并且记录下找到这两个点的路径Path1和Path2。然后,找到第一个点k满足,key1<k<key2就可以了。

如图:

假设key1 = 5,key2 = 7,那么显然,Path1{8,6,5}, Path2{8,6,7}。满足第一个key1<k<key2的k为6。故k = 6。

至于怎么求出Path1和Path2,可以看问题12。

5.如何不用递归实现二叉树的前序/后序/中序遍历?(网易面试就问到了,悲剧了,当时一下子卡住了)

看看书,基本任何一本数据结构的书都有,主要利用栈。

6.在二叉树中找出和为某一值的所有路径?

还是先解决12题目,访问二叉树到叶子节点的任意路径。这个问题解决了,自然求和看是否满足条件就可以了。

7.怎样编写一个程序,把一个有序整数数组放到二叉树中?

递归,还是利用递归:

设有int array[begin,end],首先将array[(begin + end)/2]加入二叉树,然后递归去做array[begin,(begin + end)/2 - 1]和array[(begin + end)/2 + 1, end]。注意写好函数的形式就可以了。一切都很自然。

8.判断整数序列是不是二叉搜索树的后序遍历结果

看看吧,后续遍历是这样做的:左右根,所以访问的最有一个节点实际上就是整棵二叉树的根节点root:然后,找到第一个大于该节点值的根节点b,b就是root右子树最左边的节点(大于根节点的最小节点)。那么b前面的就是root的左子树。既然是二叉搜索树的遍历结果,那么在b和root之间的遍历结果,都应该大于b。去拿这个作为判断的条件。

9.求二叉树的镜像

还是利用递归:只要节点不为空,交换左右子树的指针,然后在分别求左子树的镜像,再求右子树的镜像,直到节点为NULL。

10.一棵排序二叉树(即二叉搜索树BST),令 f=(最大值+最小值)/2,设计一个算法,找出距离f值最近、大于f值的结点。复杂度如果是O(n2)则不得分。

首先,在BST中,最小值就是最左边的节点,最大值就是最右边的节点。
在分别求出min和max后,求出f。然后利用查找,找出一个大于f的节点就可以了。
复杂度为logN。

11.把二叉搜索树转变成排序的双向链表

12..打印二叉树中的所有路径

路径的定义就是从根节点到叶子节点的点的集合。

还是利用递归:用一个list来保存经过的节点,如果已经是叶子节点了,那么打印list的所有内容;如果不是,那么将节点加入list,然后继续递归调用该函数,只不过,入口的参数变成了该节点的左子树和右子树。

程序如下:

  1. 解答1:自己看书了
  2. 解答2:
  3. //问题2:怎样从顶部开始逐层打印二叉树结点数据
  4. voidPrintAtLevel(BiTNode*root){
  5. vector<BiTNode*>vector;
  6. vector.push_back(root);
  7. while(!vector.empty()){
  8. BiTNode*tmp=vector.front();
  9. if(tmp->lchild!=NULL)
  10. vector.push_back(tmp->lchild);
  11. if(tmp->rchild!=NULL)
  12. vector.push_back(tmp->rchild);
  13. cout<<tmp->data<<endl;
  14. vector.pop_back();
  15. }
  16. }
  17. //问题3:如何判断一棵二叉树是否是平衡二叉树
  18. intisBalencedTree(treeNode*root){
  19. if(root==NULL)
  20. return0;
  21. intdepth1=getDepth(root->lchild);
  22. intdepth2=getDepth(root->rchild);
  23. if(depth1==depth2||depth1==depth2+1||depth1==depth2-1)
  24. return1;
  25. else
  26. return0;
  27. intflag1=isBalencedTree(root->lchild);
  28. intflag2=isBalencedTree(root->rchild);
  29. if(flag1&&flag2)
  30. return1;
  31. else
  32. return0;
  33. }
  34. //问题4:设计一个算法,找出二叉树上任意两个节点的最近共同父结点,复杂度如果是O(n2)
  35. 则不得分。
  36. intgetPublicAncestors(treeNode*root,intkey1,intkey2){
  37. treeNode*ptr=root;
  38. intpath1[1000];
  39. intpathLen1=0;
  40. while(ptr!=NULL){
  41. if(key1==ptr->data){
  42. path1[pathLen1]=ptr->data;
  43. pathLen1++;
  44. printArray(path1,pathLen1);
  45. break;
  46. }
  47. else
  48. if(ptr->data>key1){
  49. path1[pathLen1]=ptr->data;
  50. pathLen1++;
  51. ptr=ptr->lchild;
  52. }
  53. else
  54. if(ptr->data<key1){
  55. path1[pathLen1]=ptr->data;
  56. pathLen1++;
  57. ptr=ptr->rchild;
  58. }
  59. }
  60. ptr=root;
  61. intpath2[1000];
  62. intpathLen2=0;
  63. while(ptr!=NULL){
  64. if(key2==ptr->data){
  65. path2[pathLen2]=ptr->data;
  66. pathLen2++;
  67. printArray(path2,pathLen2);
  68. break;
  69. }
  70. else
  71. if(ptr->data>key2){
  72. path2[pathLen2]=ptr->data;
  73. pathLen2++;
  74. ptr=ptr->lchild;
  75. }
  76. else
  77. if(ptr->data<key2){
  78. path2[pathLen2]=ptr->data;
  79. pathLen2++;
  80. ptr=ptr->rchild;
  81. }
  82. }
  83. inti=pathLen1-1;
  84. //key1和key2有序,
  85. if(key2<key1){
  86. key2=key2^key1;
  87. key1=key2^key1;
  88. key2=key2^key1;
  89. }
  90. for(;i>0;i--){
  91. if(key1<path1[i]&&path1[i]<key2){
  92. intresult=path1[i];
  93. returnresult;
  94. }
  95. }
  96. }
  97. //问题6:在二叉树中找出和为某一值的所有路径
  98. voidFindPath(treeNode*root,intpath[],intpathLen,intexpectedSum,int
  99. currentSum){
  100. if(root==NULL)
  101. return;
  102. currentSum+=root->data;
  103. path[pathLen]=root->data;
  104. pathLen++;
  105. if(currentSum==expectedSum&&root->lchild==NULL&&root->rchild==
  106. NULL){
  107. printArray(path,pathLen);
  108. }
  109. if(root->lchild!=NULL){
  110. FindPath(root->lchild,path,pathLen,expectedSum,currentSum);
  111. }
  112. if(root->rchild!=NULL){
  113. FindPath(root-
  114. >rchild,path,pathLen,expectedSum,currentSum);
  115. }
  116. currentSum-=root->data;
  117. }
  118. //问题7:怎样编写一个程序,把一个有序整数数组放到二叉树中?
  119. voidcreateTreeFromArray(inta[],intbegin,intend,treeNode**root){
  120. if(begin>end)
  121. return;
  122. else{
  123. *root=(treeNode*)malloc(sizeof(treeNode));
  124. intmid=(begin+end)/2;
  125. (*root)->data=a[mid];
  126. (*root)->rchild=NULL;
  127. (*root)->lchild=NULL;
  128. createTreeFromArray(a,begin,mid-1,&(*root)->lchild);
  129. createTreeFromArray(a,mid+1,end,&(*root)->rchild);
  130. }
  131. }
  132. //问题8:判断整数序列是不是二叉搜索树的后//序遍历结果
  133. intisPostTraverse(inta[],intbegin,intend){
  134. if(begin>=end)
  135. return1;
  136. else{
  137. introot=a[end];
  138. intlroot;
  139. inti;
  140. intlocation=begin;
  141. for(i=begin;i<end;i++){
  142. if(a[i]>root){
  143. location=i;
  144. lroot=a[i];
  145. break;
  146. }
  147. }
  148. for(i=location+1;i<end;i++){
  149. if(a[i]<lroot){
  150. return0;
  151. }
  152. }
  153. intflag1=isPostTraverse(a,begin,location-1);
  154. intflag2=isPostTraverse(a,location,end-1);
  155. if(flag1&&flag2)
  156. return1;
  157. else
  158. return0;
  159. }
  160. }
  161. //问题9:求二叉树的镜像
  162. voidchangeMirror(treeNode**root){
  163. if(*root==NULL)
  164. return;
  165. else{
  166. treeNode*temp=(*root)->lchild;
  167. (*root)->lchild=(*root)->rchild;
  168. (*root)->rchild=temp;
  169. changeMirror(&(*root)->lchild);
  170. changeMirror(&(*root)->rchild);
  171. }
  172. }
  173. //问题10:10.一棵排序二叉树(即二叉搜索树BST),令f=(最大值+最小值)/2,设计一个算
  174. //法,找出距离f值最近、大于f值的结点。复杂度如果是O(n2)则不得分。
  175. intfindNearMid(treeNode**root){
  176. treeNode*ptr=*root;
  177. intmin,max;
  178. while(ptr!=NULL){
  179. min=ptr->data;
  180. ptr=ptr->lchild;
  181. }
  182. printf("theminis%d\n",min);
  183. ptr=*root;
  184. while(ptr!=NULL){
  185. max=ptr->data;
  186. ptr=ptr->rchild;
  187. }
  188. printf("themaxis%d\n",max);
  189. inthalf=(min+max)>>1;
  190. printf("halfis%d\n",half);
  191. ptr=*root;
  192. while(1){
  193. if(ptr->data<half){
  194. ptr=ptr->rchild;
  195. }
  196. else
  197. if(ptr->data>half){
  198. intresult=ptr->data;
  199. returnresult;
  200. }
  201. else
  202. {
  203. return(ptr->rchild)->data;
  204. }
  205. }
  206. }
  207. //问题12:打印二叉树中的所有路径(与题目5很相似)
  208. voidprintPathsRecur(treeNode*node,intpath[],intpathLen){
  209. if(node==NULL)
  210. return;
  211. //appendthisnodetothepatharray
  212. path[pathLen]=node->data;
  213. pathLen++;
  214. //it'saleaf,soprintthepaththatledtohere
  215. if(node->lchild==NULL&&node->rchild==NULL){
  216. printArray(path,pathLen);
  217. }else{
  218. //otherwisetrybothsubtrees
  219. printPathsRecur(node->lchild,path,pathLen);
  220. printPathsRecur(node->rchild,path,pathLen);
  221. }
  222. }
  223. voidprintPaths(treeNode*node){
  224. intpath[1000];
  225. printPathsRecur(node,path,0);
  226. }
  227. //用到的辅助函数:
  228. /**
  229. *求二叉树的深度
  230. */
  231. intgetDepth(tNoderoot){
  232. if(root==NULL)
  233. return0;
  234. else
  235. returngetDepth(root->lchild)>getLeaf(root->rchild)?1+
  236. getDepth(
  237. root->lchild):1+getDepth(root->rchild);
  238. //{
  239. //intdepthLchild=1+getDepth(root->lchild);
  240. //intdepthRchild=1+getDepth(root->rchild);
  241. //returndepthLchild>depthRchild?depthLchild:
  242. depthRchild;
  243. //}
  244. }
  245. /**
  246. *打印数组
  247. */
  248. voidprintArray(intints[],intlen){
  249. inti;
  250. for(i=0;i<len;i++){
  251. printf("%d",ints[i]);
  252. }
  253. printf("\n");
  254. }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值