递归遍历
import java.util.Stack;/*** @Description:分别用递归和非递归实现二叉树前序、中序、后序遍历* @Author: lizhouwei* @CreateDate: 2018/4/14 9:32*///前序递归public void recPreOrder(Node head) {if (head == null) {return;}System.out.print(head.value + " ");recPreOrder(head.left);recPreOrder(head.right);}//中序递归public void recInOrder(Node head) {if (head == null) {return;}recInOrder(head.left);System.out.print(head.value + " ");recInOrder(head.right);}//后序递归public void recPostOrder(Node head) {if (head == null) {return;}recPostOrder(head.left);recPostOrder(head.right);System.out.print(head.value + " ");}
前序非递归
//------------非递归----------------////前序public void preOrder(Node head) {if (head == null) {return;}Stack<Node> stack = new Stack<Node>();stack.push(head);while (!stack.isEmpty()) {head = stack.pop();System.out.print(head.value + " ");if (head.right != null) {stack.push(head.right);}if (head.left != null) {stack.push(head.left);}}}
中序非递归
//中序public void inOrder(Node head) {if (head == null) {return;}Stack<Node> stack = new Stack<Node>();while (!stack.isEmpty() || head != null) {if (head != null) {stack.push(head);head = head.left;} else {head = stack.pop();System.out.print(head.value + " ");head = head.right;}}}//中序public void inOrder(Node head) {if (head == null) {return;}Stack<Node> stack = new Stack<Node>();while (!stack.isEmpty() || head != null) {while (head != null) {stack.push(head);head = head.left;}head = stack.pop();System.out.print(head.value + " ");head = head.right;}}
后序非递归
//后序public void postOrder(Node head) {if (head == null) {return;}Node cur = null;Stack<Node> stack = new Stack<Node>();stack.push(head);while (!stack.isEmpty()) {cur = stack.peek();if (cur.left != null && cur.left != head && cur.right != head) {stack.push(cur.left);} else if (cur.right != null && cur.right != head) {stack.push(cur.right);} else {head = stack.pop();System.out.print(head.value + " ");}}}//后序遍历public void postOrder(Node head){if(head == null) return;Node pre = null;//当前节点的之前访问的节点Node cur;Stack<Node> stack = new Stack<>();stack.push(root);while(!stack.isEmpty()){cur = stack.peek();//当前节点是叶子节点,可以直接访问该节点//如果前一个节点不为空并且是当前节点的左孩子或者右孩子,// -->当是左孩子时说明当前节点右孩子为空,// -->当是右孩子时,说明左右孩子都访问过了,且都不为空if((cur.left == null && cur.right == null) ||(pre != null &&(pre == cur.left|| pre == cur.right))){System.out.print(cur.val + "-->");pre = stack.pop();}else{//当前节点为栈顶元素 如果当前节点不是叶子节点,// -->在当前节点之前访问的那个节点不是当前节点的孩子,则进行压栈//先压栈右节点再压栈左节点 这样出栈时是先左后右if(cur.right != null) {stack.push(cur.right);}if(cur.left != null){stack.push(cur.left);}}}}
测试
//测试public static void main(String[] args) {Chapter3_1 chapter = new Chapter3_1();int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};Node head = NodeUtil.generTree(arr, 0, arr.length - 1);//前序递归打印System.out.print("前序递归打印:");chapter.recPreOrder(head);System.out.println();//前序非递归打印System.out.print("前序非递归打印:");chapter.preOrder(head);System.out.println();System.out.println();//中序递归打印System.out.print("中序递归打印:");chapter.recInOrder(head);System.out.println();System.out.print("中序非递归打印:");chapter.inOrder(head);System.out.println();System.out.println();//后序递归打印System.out.print("后序递归打印:");chapter.recPostOrder(head);System.out.println();System.out.print("后序非递归打印:");chapter.postOrder(head);}
