劍指Offer面試題6:重建二叉樹 Java實現

題目:重建二叉樹
          輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複數字。例如,輸入前序遍歷序列{1,2,4,7,3,5,6,8}和中序遍歷序列{4,7,2,1,5,3,8,6},則重建出圖中所示的二叉樹並輸出它的頭節點。

算法分析:
在二叉樹的前序遍歷序列中,第一個數字總是樹的根節點的值。但在中序遍歷中,根節點的值在序列的中間,左子樹的結點的值位於根節點的值的左邊,而右子樹的結點的值位於根節點的右邊。因此我們需要掃描中序遍歷序列,才能找到根節點的值。
 
如圖所示,前序遍歷序列的第一個數字1就是根節點的值。掃描中序遍歷序列,就能確定根節點的值的位置。根據中序遍歷的特點,在根節點的值1前面3個數字都是左子樹結點的值,位於1後面的數字都是右子樹結點的值。
 
由於中序遍歷序列中,有3個數字是左子樹結點的值,因此左子樹總共有3個左子結點。同樣,在前序遍歷的序列中,根節點後面的3個數字就是3個左子樹結點的值,再後面的所有數字都是右子樹結點的值。這樣我們就在前序遍歷和中序遍歷兩個序列中,分別找到了左右子樹對應的子序列。
 
 
既然我們已經分別找到了左、右子樹的前序遍歷序列和中序遍歷序列,我們可以用同樣的方法分別去構建左右子樹。也就是說,接下來的事情可以用遞歸的方法去完成。


算法源程序;

  1. /**************************************************************
  2. * Copyright (c) 2016, 北京郵電大學
  3. * All rights reserved.
  4. * 版 本 號:v1.0
  5. * 題目描述:重建二叉樹
  6. * 輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複數字。
  7. * 例如,輸入前序遍歷序列{1,2,4,7,3,5,6,8}和中序遍歷序列{4,7,2,1,5,3,8,6},則重建出圖中所示的二叉樹並輸出它的頭節點。
  8. * 輸入描述:無
  9. * 程序輸出: 4 7 2 1 5 3 8 6 Exception in thread "main"
  10. * 5 4 3 2 1
  11. * 1 2 3 4 5
  12. * 1
  13. * 4 2 5 1 6 3 7
  14. * 問題分析:無
  15. * 算法描述:無
  16. * 完成時間:2016-10-30
  17. ***************************************************************/
  18. package org.marsguo.offerproject06;
  19. /*
  20. 定義二叉樹節點
  21. */
  22. class TreeNode{
  23. int val;
  24. public TreeNode left = null;
  25. public TreeNode right = null;
  26. }
  27. class SolutionMethod1{
  28. /**
  29. * 輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二節樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。
  30. *
  31. * @param preorder 前序遍歷
  32. * @param inorder 中序遍歷
  33. * @return 樹的根結點
  34. */
  35. public TreeNode BinaryTreeFunction(int[] preorder,int[] inorder){
  36. if(preorder == null || inorder == null ||
  37. preorder.length != inorder.length || inorder.length < 1){
  38. return null;
  39. }
  40. return ConstructCore(preorder,0,preorder.length - 1,inorder,0,inorder.length - 1);
  41. }
  42. /**
  43. * 輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二節樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。
  44. *
  45. * @param preorder 前序遍歷
  46. * @param ps 前序遍歷的開始位置
  47. * @param pe 前序遍歷的結束位置
  48. * @param inorder 中序遍歷
  49. * @param is 中序遍歷的開始位置
  50. * @param ie 中序遍歷的結束位置
  51. * @return 樹的根結點
  52. */
  53. public TreeNode ConstructCore(int[] preorder, int ps, int pe, int[] inorder, int is, int ie){
  54. if(ps > pe){ //開始位置大於結束位置,說明已經沒有需要處理的元素了
  55. return null;
  56. }
  57. int value = preorder[ps]; // 取前序遍歷的第一個數字,就是當前的根結點
  58. int index = is;
  59. while(index <= ie && inorder[index] != value){ // 在中序遍歷的數組中找根結點的位置
  60. index++;
  61. }
  62. if(index > ie){ // 如果在整個中序遍歷的數組中沒有找到,說明輸入的參數是不合法的,拋出異常
  63. throw new RuntimeException("Invalid input");
  64. }
  65. TreeNode node = new TreeNode(); // 創建當前的根結點,並且爲結點賦值
  66. node.val = value;
  67. /*
  68. 遞歸構建當前根結點的左子樹,左子樹的元素個數:index-is+1個
  69. 左子樹對應的前序遍歷的位置在[ps+1, ps+index-is]
  70. 左子樹對應的中序遍歷的位置在[is, index-1]
  71. */
  72. node.left = ConstructCore(preorder, ps + 1, ps + index - is, inorder, is, index - 1);
  73. /*遞歸構建當前根結點的右子樹,右子樹的元素個數:ie-index個
  74. 右子樹對應的前序遍歷的位置在[ps+index-is+1, pe]
  75. 右子樹對應的中序遍歷的位置在[index+1, ie]
  76. */
  77. node.right = ConstructCore(preorder, ps + index - is + 1, pe, inorder, index + 1, ie);
  78. // 返回創建的根結點
  79. return node;
  80. }
  81. public void printTree(TreeNode root){ // 中序遍歷二叉樹
  82. if(root != null){
  83. printTree(root.left);
  84. System.out.print(root.val + " ");
  85. printTree(root.right);
  86. }
  87. //System.out.println();
  88. }
  89. // 普通二叉樹
  90. // 1
  91. // / \
  92. // 2 3
  93. // / / \
  94. // 4 5 6
  95. // \ /
  96. // 7 8
  97. public void test1() {
  98. int[] preorder = {1, 2, 4, 7, 3, 5, 6, 8};
  99. int[] inorder = {4, 7, 2, 1, 5, 3, 8, 6};
  100. TreeNode root = BinaryTreeFunction(preorder, inorder);
  101. printTree(root);
  102. }
  103. // 所有結點都沒有右子結點
  104. // 1
  105. // /
  106. // 2
  107. // /
  108. // 3
  109. // /
  110. // 4
  111. // /
  112. // 5
  113. public void test2() {
  114. int[] preorder = {1, 2, 3, 4, 5};
  115. int[] inorder = {5, 4, 3, 2, 1};
  116. TreeNode root = BinaryTreeFunction(preorder, inorder);
  117. printTree(root);
  118. }
  119. // 所有結點都沒有左子結點
  120. // 1
  121. // \
  122. // 2
  123. // \
  124. // 3
  125. // \
  126. // 4
  127. // \
  128. // 5
  129. public void test3() {
  130. int[] preorder = {1, 2, 3, 4, 5};
  131. int[] inorder = {1, 2, 3, 4, 5};
  132. TreeNode root = BinaryTreeFunction(preorder, inorder);
  133. printTree(root);
  134. }
  135. // 樹中只有一個結點
  136. public void test4() {
  137. int[] preorder = {1};
  138. int[] inorder = {1};
  139. TreeNode root = BinaryTreeFunction(preorder, inorder);
  140. printTree(root);
  141. }
  142. // 完全二叉樹
  143. // 1
  144. // / \
  145. // 2 3
  146. // / \ / \
  147. // 4 5 6 7
  148. public void test5() {
  149. int[] preorder = {1, 2, 4, 5, 3, 6, 7};
  150. int[] inorder = {4, 2, 5, 1, 6, 3, 7};
  151. TreeNode root = BinaryTreeFunction(preorder, inorder);
  152. printTree(root);
  153. }
  154. // 輸入空指針
  155. public void test6() {
  156. BinaryTreeFunction(null, null);
  157. }
  158. // 輸入的兩個序列不匹配
  159. public void test7() {
  160. int[] preorder = {1, 2, 4, 5, 3, 6, 7};
  161. int[] inorder = {4, 2, 8, 1, 6, 3, 7};
  162. TreeNode root = BinaryTreeFunction(preorder, inorder);
  163. printTree(root);
  164. }
  165. }
  166. public class RebuildBinaryTree {
  167. public static void main(String[] args){
  168. SolutionMethod1 solution1 = new SolutionMethod1();
  169. solution1.test1();
  170. System.out.println();
  171. solution1.test2();
  172. System.out.println();
  173. solution1.test3();
  174. System.out.println();
  175. solution1.test4();
  176. System.out.println();
  177. solution1.test5();
  178. System.out.println();
  179. solution1.test6();
  180. System.out.println();
  181. solution1.test7();
  182. System.out.println();
  183. }
  184. }


程序運行結果

 
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章