二叉树的遍历(递归与非递归版本)

梦里梦外; 2022-04-14 04:35 294阅读 0赞

最近在写关于二叉树方面的题目的时候,总是会用到二叉树的各种遍历,所以在这里将自己写的各种遍历,都记录下来.

递归部分:
首先二叉树的递归代码是比较简单的,而且前序,中序和后序遍历代码几乎一样, 只是打印节点值的输出语句的位置不一样.
可以递归部分,对于二叉树中节点的遍历顺序是一样的, 都是先遍历当前节点,然后遍历当前节点的左子树,然后是右子树, 然后在遍历节点的过程, 选择输出的节点值的时机.

非递归部分:
其实思想还是参考递归部分, 只是将递归遍历节点部分, 变成了使用栈来存放遍历的节点, 然后选择打印节点的值的时机

以下是代码部分

  1. import java.util.LinkedList;
  2. import java.util.Queue;
  3. import java.util.Stack;
  4. /**
  5. * author: zzw5005
  6. * date: 2018/11/24 9:57
  7. */
  8. public class TraversalBST {
  9. //递归版本的二叉树遍历
  10. public static void preOrder(TreeNode root){
  11. if(root == null){
  12. return;
  13. }
  14. System.out.print(root.val + " ");
  15. preOrder(root.left);
  16. preOrder(root.right);
  17. }
  18. public static void inOrder(TreeNode root){
  19. if(root == null){
  20. return;
  21. }
  22. inOrder(root.left);
  23. System.out.print(root.val + " ");
  24. inOrder(root.right);
  25. }
  26. public static void posOrder(TreeNode root){
  27. if(root == null){
  28. return;
  29. }
  30. posOrder(root.left);
  31. posOrder(root.right);
  32. System.out.print(root.val + " ");
  33. }
  34. //非递归版本的二叉树遍历
  35. public static void preOrder1(TreeNode root){
  36. Stack<TreeNode> stack = new Stack<>();
  37. while(!stack.isEmpty() || root != null){
  38. while(root != null){
  39. System.out.print(root.val + " ");
  40. stack.push(root);
  41. root = root.left;
  42. }
  43. if(!stack.isEmpty()){
  44. root = stack.pop();
  45. root = root.right;
  46. }
  47. }
  48. }
  49. public static void inOrder1(TreeNode root){
  50. Stack<TreeNode> stack = new Stack<>();
  51. while(!stack.isEmpty() || root != null){
  52. while(root != null){
  53. stack.push(root);
  54. root = root.left;
  55. }
  56. if(!stack.isEmpty()){
  57. root = stack.pop();
  58. System.out.print(root.val + " ");
  59. root = root.right;
  60. }
  61. }
  62. }
  63. public static void posOrder1(TreeNode root){
  64. Stack<TreeNode> stack1 = new Stack<>();
  65. Stack<Integer> stack2 = new Stack<>();
  66. while(!stack1.isEmpty() || root != null){
  67. while(root != null){
  68. stack1.push(root);
  69. stack2.push(0);
  70. root = root.left;
  71. }
  72. while(!stack1.isEmpty() && stack2.peek() == 1){
  73. stack2.pop();
  74. System.out.print(stack1.pop().val + " ");
  75. }
  76. if(!stack1.isEmpty()){
  77. stack2.pop();
  78. stack2.push(1);
  79. root = stack1.peek();
  80. root = root.right;
  81. }
  82. }
  83. }
  84. public static void levOrder(TreeNode root){
  85. if(root == null){
  86. return;
  87. }
  88. Queue<TreeNode> queue = new LinkedList<>();
  89. queue.add(root);
  90. while(!queue.isEmpty()){
  91. root = queue.poll();
  92. System.out.print(root.val + " ");
  93. if(root.left != null){
  94. queue.add(root.left);
  95. }
  96. if(root.right != null){
  97. queue.add(root.right);
  98. }
  99. }
  100. }
  101. public static class TreeNode {
  102. int val = 0;
  103. TreeNode left = null;
  104. TreeNode right = null;
  105. public TreeNode(int val) {
  106. this.val = val;
  107. }
  108. }
  109. public static void main(String[] args) {
  110. //树
  111. TreeNode root = new TreeNode(8);
  112. root.left = new TreeNode(6);
  113. root.right = new TreeNode(10);
  114. //root.left = new TreeNode(6);
  115. root.left.left = new TreeNode(5);
  116. root.left.right = new TreeNode(7);
  117. //root.left.right = new TreeNode(10);
  118. root.right.left = new TreeNode(9);
  119. root.right.right = new TreeNode(11);
  120. System.out.println("递归版本的二叉树遍历 : ");
  121. System.out.print("preOrder : ");
  122. preOrder(root);
  123. System.out.println();
  124. System.out.print("inOrder : ");
  125. inOrder(root);
  126. System.out.println();
  127. System.out.print("posOrder : ");
  128. posOrder(root);
  129. System.out.println("\n");
  130. System.out.println("非递归版本的二叉树遍历 : ");
  131. System.out.print("preOrder1 : ");
  132. preOrder1(root);
  133. System.out.println();
  134. System.out.print("inOrder1 : ");
  135. inOrder1(root);
  136. System.out.println();
  137. System.out.print("posOrder1 : ");
  138. posOrder1(root);
  139. System.out.println();
  140. System.out.print("levOrder : ");
  141. levOrder(root);
  142. System.out.println();
  143. }
  144. }

输出的结果

  1. 递归版本的二叉树遍历 :
  2. preOrder : 8 6 5 7 10 9 11
  3. inOrder : 5 6 7 8 9 10 11
  4. posOrder : 5 7 6 9 11 10 8
  5. 非递归版本的二叉树遍历 :
  6. preOrder1 : 8 6 5 7 10 9 11
  7. inOrder1 : 5 6 7 8 9 10 11
  8. posOrder1 : 5 7 6 9 11 10 8
  9. levOrder : 8 6 10 5 7 9 11

发表评论

表情:
评论列表 (有 0 条评论,294人围观)

还没有评论,来说两句吧...

相关阅读