二叉树递归及非递归遍历
1 package com.company; 2 3 import java.util.ArrayList; 4 import java.util.LinkedList; 5 import java.util.List; 6 import java.util.Queue; 7 import java.util.Stack; 8 9 /** 10 * @author wuzhijun <wuzhijun@kuaishou.com> 11 * Created on 2022-01-13 12 */ 13 public class TreeNodeOrder { 14 public static void main(String[] args) { 15 // https://www.cnblogs.com/liuyang0/p/6271331.html 16 17 System.out.println("前序遍历"); 18 preOrderRecur(buildTree()); 19 preOrder(buildTree()); 20 System.out.println("中序遍历"); 21 midOrderRecur(buildTree()); 22 midOrder(buildTree()); 23 System.out.println("后续遍历"); 24 postOrderRecur(buildTree()); 25 posOrder(buildTree()); 26 System.out.println("层级遍历"); 27 levelOrderRecur(buildTree()); 28 levelOrder(buildTree()); 29 } 30 31 private static TreeNode buildTree() { 32 33 List<TreeNode> nodes = new ArrayList<>(); 34 for (int i = 0; i <= 10; i++) { 35 nodes.add(new TreeNode(i)); 36 } 37 nodes.get(4).setLeft(nodes.get(2)); 38 nodes.get(4).setRight(nodes.get(6)); 39 nodes.get(2).setLeft(nodes.get(1)); 40 nodes.get(2).setRight(nodes.get(3)); 41 nodes.get(6).setLeft(nodes.get(5)); 42 nodes.get(6).setRight(nodes.get(7)); 43 nodes.get(7).setRight(nodes.get(8)); 44 nodes.get(8).setRight(nodes.get(10)); 45 return nodes.get(4); 46 47 } 48 49 //递归前序遍历 50 private static void preOrderRecur(TreeNode node) { 51 if (node == null) { 52 return; 53 } 54 System.out.println(node.value); 55 preOrderRecur(node.left); 56 preOrderRecur(node.right); 57 } 58 59 //递归中序遍历 60 private static void midOrderRecur(TreeNode node) { 61 if (node == null) { 62 return; 63 } 64 midOrderRecur(node.left); 65 System.out.println(node.value); 66 midOrderRecur(node.right); 67 } 68 69 //递归后序遍历 70 private static void postOrderRecur(TreeNode node) { 71 if (node == null) { 72 return; 73 } 74 postOrderRecur(node.left); 75 postOrderRecur(node.right); 76 System.out.println(node.value); 77 } 78 79 //层序遍历 80 private static void levelOrderRecur(TreeNode node) { 81 if (node == null) { 82 return; 83 } 84 int depth = depth(node); 85 for (int i = 1; i <= depth; i++) { 86 levelOrderRecur(node, i); 87 } 88 } 89 90 private static void levelOrderRecur(TreeNode node, int level) { 91 if (node == null || level < 1) { 92 return; 93 } 94 if (level == 1) { 95 System.out.println(node.value); 96 } 97 levelOrderRecur(node.left, level - 1); 98 levelOrderRecur(node.right, level - 1); 99 } 100 101 private static int depth(TreeNode node) { 102 if (node == null) { 103 return 0; 104 } 105 int leftDepth = depth(node.left); 106 int rightDepth = depth(node.right); 107 return Math.max(leftDepth, rightDepth) + 1; 108 } 109 110 111 //非递归先序遍历 112 private static void preOrder(TreeNode node) { 113 if (node == null) { 114 return; 115 } 116 Stack<TreeNode> stack = new Stack<>(); 117 stack.push(node); 118 while (!stack.isEmpty()) { 119 TreeNode item = stack.pop(); 120 System.out.println(item.getValue()); 121 if (item.getRight() != null) { 122 stack.push(item.getRight()); 123 } 124 if (item.getLeft() != null) { 125 stack.push(item.getLeft()); 126 } 127 } 128 } 129 130 //非递归中序遍历 131 private static void midOrder(TreeNode node) { 132 if (node == null) { 133 return; 134 } 135 Stack<TreeNode> stack = new Stack<>(); 136 while (!stack.isEmpty() || node != null) { 137 while (node != null) { 138 stack.push(node); 139 node = node.getLeft(); 140 } 141 if (!stack.isEmpty()) { 142 TreeNode item = stack.pop(); 143 System.out.println(item.getValue()); 144 node = item.getRight(); 145 } 146 } 147 } 148 149 // 非递归后序遍历 150 private static void posOrder(TreeNode head) { 151 if (head == null) { 152 return; 153 } 154 Stack<TreeNode> s1 = new Stack<>(); 155 Stack<TreeNode> s2 = new Stack<>(); 156 s1.push(head); 157 while (!s1.isEmpty()) { 158 TreeNode cur = s1.pop(); 159 s2.push(cur); 160 if (cur.left != null) { 161 s1.push(cur.left); 162 } 163 if (cur.right != null) { 164 s1.push(cur.right); 165 } 166 } 167 while (!s2.isEmpty()) { 168 TreeNode cur = s2.pop(); 169 System.out.println(cur.getValue()); 170 } 171 } 172 173 174 private static void levelOrder(TreeNode node) { 175 if (node == null) { 176 return; 177 } 178 Queue<TreeNode> queue = new LinkedList<>(); 179 queue.add(node); 180 while (!queue.isEmpty()) { 181 TreeNode item = queue.poll(); 182 System.out.println(item.getValue()); 183 if (item.getLeft() != null) { 184 queue.add(item.getLeft()); 185 } 186 if (item.getRight() != null) { 187 queue.add(item.getRight()); 188 } 189 } 190 } 191 192 }