求二叉树的深度(二叉树,孩子,算法,编程语言)

时间:2024-05-02 15:10:16 作者 : 石家庄SEO 分类 : 编程语言
  • TAG :

对于二叉树的最大的深度,可以采用递归算法。
算法描述如下:
如果根结点为null,那么深度=0
如果根结点不是null,那么就看该当前结点的左孩子的深度和右孩子的深度
如果左孩子深度>=右孩子的深度,那么当前根结点的深度就是左孩子的深度+1.
反之则为右孩子的深度+1

对每个左孩子右孩子也是采用同样的算法。到某一节点是null的时候,才能返回0;

之前的文章有关于二叉树遍历的算法的描述。此处,对于遍历可以做一些小的改进,使它可以在遍历的时候计算出当前节点的深度。只要在递归方法中加入一个深度参数,每次调用的递归方法的时候,该参数都会自增1.则可以计算出深度。

假设构造出2棵树

求二叉树的深度

字母树

求二叉树的深度

数字树

采用算法计算深度,和遍历。

结果如下:

求二叉树的深度

具体代码如下:

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. namespacetree
  6. {
  7. #region节点的定义
  8. classnode
  9. {
  10. publicstringnodevalue;
  11. publicnodeleftchild,rightchild;
  12. publicnode()
  13. {}
  14. publicnode(stringvalue)
  15. {
  16. nodevalue=value;
  17. }
  18. publicvoidassignchild(nodeleft,noderight)//设定左右孩子
  19. {
  20. this.leftchild=left;
  21. this.rightchild=right;
  22. }
  23. publicboolhasleftchild//是否有左孩子
  24. {
  25. get
  26. {
  27. return(leftchild!=null);
  28. }
  29. }
  30. publicboolhasrightchild//是否有右孩子
  31. {
  32. get
  33. {
  34. return(rightchild!=null);
  35. }
  36. }
  37. publicboolhaschild//是否有右孩子
  38. {
  39. get
  40. {
  41. returnhasleftchild||hasrightchild;
  42. }
  43. }
  44. }
  45. #endregion
  46. classProgram
  47. {
  48. staticvoidMain(string[]args)
  49. {
  50. nodenode_a=newnode("a");
  51. nodenode_b=newnode("b");
  52. nodenode_c=newnode("c");
  53. nodenode_d=newnode("d");
  54. nodenode_e=newnode("e");
  55. nodenode_f=newnode("f");
  56. nodenode_g=newnode("g");
  57. nodenode_h=newnode("h");
  58. nodenode_i=newnode("i");
  59. //构造一棵二叉树
  60. node_a.assignchild(node_b,node_c);
  61. node_b.assignchild(node_d,node_e);
  62. node_c.assignchild(node_f,node_g);
  63. node_e.assignchild(node_h,node_i);
  64. Console.WriteLine("maxDepth:"+maxDepth(node_a));
  65. preorder_visit_depth(node_a,1);
  66. Console.WriteLine();
  67. nodenode_1=newnode("1");
  68. nodenode_2=newnode("2");
  69. nodenode_3=newnode("3");
  70. nodenode_4=newnode("4");
  71. nodenode_5=newnode("5");
  72. nodenode_6=newnode("6");
  73. nodenode_7=newnode("7");
  74. nodenode_8=newnode("8");
  75. nodenode_9=newnode("9");
  76. nodenode_10=newnode("10");
  77. nodenode_11=newnode("11");
  78. nodenode_12=newnode("12");
  79. nodenode_13=newnode("13");
  80. //构造一棵二叉树
  81. node_1.assignchild(node_2,node_3);
  82. node_2.assignchild(node_4,node_5);
  83. node_3.assignchild(null,node_7);
  84. node_5.assignchild(node_6,null);
  85. node_7.assignchild(node_8,null);
  86. node_8.assignchild(node_9,null);
  87. node_9.assignchild(node_10,node_11);
  88. node_10.assignchild(null,node_12);
  89. node_6.assignchild(null,node_13);
  90. Console.WriteLine("maxDepth:"+maxDepth(node_1));
  91. preorder_visit_depth(node_1,1);
  92. Console.WriteLine();
  93. }
  94. //计算深度
  95. staticintmaxDepth(noderoot)
  96. {
  97. if(root==null)
  98. {
  99. return0;
  100. }
  101. else
  102. {
  103. intleftdepth=maxDepth(root.leftchild);//递归计算左孩子的深度
  104. intrightdepth=maxDepth(root.rightchild);//递归计算右孩子的深度
  105. if(leftdepth>=rightdepth)
  106. {
  107. returnleftdepth+1;
  108. }
  109. else
  110. {
  111. returnrightdepth+1;
  112. }
  113. }
  114. }
  115. //先序遍历//DLR
  116. staticvoidpreorder_visit_depth(nodeAnode,intdepth)
  117. {
  118. Console.WriteLine(Anode.nodevalue+"-depth:"+depth);
  119. depth++;
  120. if(Anode.hasleftchild)
  121. {
  122. preorder_visit_depth(Anode.leftchild,depth);
  123. }
  124. if(Anode.hasrightchild)
  125. {
  126. preorder_visit_depth(Anode.rightchild,depth);
  127. }
  128. }
  129. }
  130. }

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:求二叉树的深度的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:UICollectionview Xib 行间距下一篇:

5 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18