三种遍历方式
1.先序:根左右
2.后序:左右根
3.中序:左根右
先定义一个二叉树类
class TreeNode { int value; TreeNode left; TreeNode right;
public TreeNode(){} public TreeNode(int value, TreeNode left, TreeNode right) { this.value = value; this.left = left; this.right = right; } public TreeNode(int value) { this.value = value; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public TreeNode getLeft() { return left; } public void setLeft(TreeNode left) { this.left = left; } public TreeNode getRight() { return right; } public void setRight(TreeNode right) { this.right = right; } }
递归遍历
//先序递归遍历 private static void recursionPreOrder(TreeNode root) { if (root==null){ return; } System.out.print(root.value); recursionPreOrder(root.left); recursionPreOrder(root.right); } //后序递归遍历 private static void recursionBackOrder(TreeNode root) { if (root==null){ return; } recursionBackOrder(root.left); recursionBackOrder(root.right); System.out.print(root.value); } //中序递归遍历 private static void recursionMiddleOrder(TreeNode root) { if (root==null){ return; } recursionMiddleOrder(root.left); System.out.print(root.value); recursionMiddleOrder(root.right); }
非递归式遍历
因为递归方法是将栈的管理交给了系统,非递归式则需要我们自己去管理,所以我们使用栈去实现它
//非递归式 //先序 private static void preOrder(TreeNode root) { Stack<TreeNode> nodeStack = new Stack<>(); while (root != null || !nodeStack.isEmpty()) { while (root != null) { System.out.println(root.value); nodeStack.push(root); root = root.left; } if (!nodeStack.isEmpty()) { root = nodeStack.pop().right; } } } //中序 private static void middleOrder(TreeNode root) { Stack<TreeNode> nodeStack = new Stack<>(); while (root != null || !nodeStack.isEmpty()) { while (root != null) { nodeStack.push(root); root = root.left; } if (!nodeStack.isEmpty()) { root = nodeStack.pop(); System.out.println(root.value); root = root.right; } } } //后序 private static void backOrder(TreeNode root) { Stack<TreeNode> nodeStack = new Stack<>(); TreeNode lastNode = root; while (root != null || !nodeStack.isEmpty()) { while (root != null) { nodeStack.push(root); root = root.left; } root = nodeStack.peek(); if (root.right == null || root.right == lastNode) { System.out.println(root.value); nodeStack.pop(); lastNode = root; root = null; } else { root = root.right; } } }
测试代码
class TreeNode { int value; TreeNode left; TreeNode right; public TreeNode() { } public TreeNode(int value, TreeNode left, TreeNode right) { this.value = value; this.left = left; this.right = right; } public TreeNode(int value) { this.value = value; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public TreeNode getLeft() { return left; } public void setLeft(TreeNode left) { this.left = left; } public TreeNode getRight() { return right; } public void setRight(TreeNode right) { this.right = right; } }
public static void main(String args[]) { //二叉树遍历 TreeNode node1 = new TreeNode(7); TreeNode node2 = new TreeNode(8); TreeNode node3 = new TreeNode(6, node1, node2); TreeNode node4 = new TreeNode(4); node4.right = node3; TreeNode node5 = new TreeNode(2); node5.left = node4; TreeNode root = new TreeNode(1); root.left = node5; TreeNode node6 = new TreeNode(5); TreeNode node7 = new TreeNode(3); node7.right = node6; root.right = node7; // System.out.println("先序"); // recursionPreOrder(root); // System.out.println(); // System.out.println("后序"); // recursionBackOrder(root); // System.out.println(); // System.out.println("中序"); // recursionMiddleOrder(root); // preOrder(root); backOrder(root); // middleOrder(root); // postorderTraversal(root); }