当前位置:   article > 正文

前序,中序,后序和层序遍历(Java)的n中写法_java 中序遍历 英文

java 中序遍历 英文

 

前序,中序和后序除递归方法外,推荐前序非递归3,中序非递归2,后序非递归(这个以后要回来看,明天怕是就会忘掉),层序以后可以关注一下有没有递归的方法。

  1. class TreeNode{
  2. int val;
  3. TreeNode left,right;
  4. public TreeNode(int val) {
  5. super();
  6. this.val = val;
  7. }
  8. public TreeNode(int val, TreeNode left, TreeNode right) {
  9. super();
  10. this.val = val;
  11. this.left = left;
  12. this.right = right;
  13. }
  14. //前序递归
  15. public static String preOrder(TreeNode root) {
  16. StringBuilder builder=new StringBuilder();
  17. preOrderHelper(root, builder);
  18. return builder.toString();
  19. }
  20. public static void preOrderHelper(TreeNode n,StringBuilder builder) {
  21. if(n==null)return;
  22. //如果遇到需要判断叶子结点的需要以下代码
  23. // TreeNode left=n.left;
  24. // TreeNode right=n.right;
  25. // if(left==null&&right==null)return;
  26. builder.append(n.val+" ");
  27. preOrderHelper(n.left, builder);
  28. preOrderHelper(n.right,builder);
  29. }
  30. //前序非递归1
  31. public static String preOrderNonRecursive1(TreeNode root) {
  32. StringBuilder builder=new StringBuilder();
  33. Stack<TreeNode> stack=new Stack<>();
  34. stack.push(root);
  35. while(stack.size()!=0) {
  36. TreeNode n=stack.pop();
  37. if(n==null)continue;
  38. builder.append(n.val+" ");
  39. stack.push(n.right);
  40. stack.push(n.left);
  41. }
  42. return builder.toString();
  43. }
  44. //前序非递归2
  45. public static String preOrderNonRecursive2(TreeNode root) {
  46. StringBuilder builder=new StringBuilder();
  47. Stack<TreeNode> stack=new Stack<>();
  48. while(true) {
  49. while(root!=null) {
  50. builder.append(root.val+" ");
  51. stack.push(root);
  52. root=root.left;
  53. }
  54. if(stack.isEmpty())break;
  55. root=stack.pop();
  56. root=root.right;
  57. }
  58. return builder.toString();
  59. }
  60. //前序非递归写法3
  61. public static String preOrderNonRecursive3(TreeNode root) {
  62. StringBuilder builder=new StringBuilder();
  63. Stack<TreeNode> stack=new Stack<>();
  64. while(root!=null||!stack.isEmpty()) {
  65. if(root!=null) {
  66. stack.push(root);
  67. builder.append(root.val);
  68. root=root.left;
  69. }
  70. else {
  71. root=stack.pop();
  72. root=root.right;
  73. }
  74. }
  75. return builder.toString();
  76. }
  77. //中序遍历递归
  78. public static String inOrderRecursive(TreeNode root) {
  79. StringBuilder builder=new StringBuilder();
  80. inOrderHelper(root, builder);
  81. return builder.toString();
  82. }
  83. public static void inOrderHelper(TreeNode n,StringBuilder builder) {
  84. if(n==null)return;
  85. inOrderHelper(n.left, builder);
  86. builder.append(n.val+" ");
  87. inOrderHelper(n.right, builder);
  88. }
  89. //中序遍历非递归写法1
  90. public static String inOrderNonRecursive(TreeNode root) {
  91. Stack<TreeNode> stack=new Stack<>();
  92. StringBuilder builder=new StringBuilder();
  93. while(true) {
  94. while(root!=null) {
  95. stack.push(root);
  96. root=root.left;
  97. }
  98. if(stack.isEmpty())break;
  99. root=stack.pop();
  100. builder.append(root.val+" ");
  101. root=root.right;
  102. }
  103. return builder.toString();
  104. }
  105. //中序非递归写法2
  106. public static String inOrderNonRecursive2(TreeNode root) {
  107. StringBuilder builder=new StringBuilder();
  108. Stack<TreeNode> stack=new Stack<>();
  109. while(root!=null||!stack.isEmpty()) {
  110. if(root!=null) {
  111. stack.push(root);
  112. root=root.left;
  113. }else {
  114. root=stack.pop();
  115. builder.append(root.val);
  116. root=root.right;
  117. }
  118. }
  119. return builder.toString();
  120. }
  121. //后序递归
  122. public static String postOrderRecursive(TreeNode root) {
  123. StringBuilder builder=new StringBuilder();
  124. postOrderHelper(root, builder);
  125. return builder.toString();
  126. }
  127. public static void postOrderHelper(TreeNode n,StringBuilder builder) {
  128. if(n==null)return;
  129. postOrderHelper(n.left, builder);
  130. postOrderHelper(n.right, builder);
  131. builder.append(n.val);
  132. }
  133. //后序非递归
  134. public static String postOrderNonRecursive(TreeNode root) {
  135. Stack<TreeNode> stack=new Stack<>();
  136. StringBuilder builder=new StringBuilder();
  137. TreeNode pre=null,curr;
  138. stack.push(root);
  139. while(!stack.isEmpty()) {
  140. curr=stack.peek();
  141. if((curr.left==null&&curr.right==null)||(pre!=null&&(pre==curr.left||pre==curr.right))) {
  142. builder.append(curr.val);
  143. stack.pop();
  144. pre=curr;
  145. }else {
  146. if(curr.right!=null)
  147. stack.push(curr.right);
  148. if(curr.left!=null)
  149. stack.push(curr.left);
  150. }
  151. }
  152. return builder.toString();
  153. }
  154. //层序遍历好像还没有递归的方法,因为他一层一层,有很多结点。
  155. public static String BFS(TreeNode root) {
  156. StringBuilder builder=new StringBuilder();
  157. LinkedList<TreeNode> queue=new LinkedList<>();
  158. queue.offer(root);
  159. while(!queue.isEmpty()) {
  160. TreeNode n=queue.poll();
  161. if(n==null)continue;
  162. builder.append(n.val);
  163. queue.offer(n.left);
  164. queue.offer(n.right);
  165. }
  166. return builder.toString();
  167. }
  168. @Override
  169. public String toString() {
  170. return "TreeNode [val=" + val +"]";
  171. }
  172. }
  • 利用了上面的中序遍历非递归方法3,完成了题目783。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/558351
推荐阅读
相关标签
  

闽ICP备14008679号