二叉树的遍历广义上是指下面我们说的七种遍历
广度优先搜索 : 递归完成 前序 中序 后序 的遍历
深度优先搜索 : 层序遍历(借助队列)
非递归的迭代法完成前中后遍历(借助栈)
代码合集如下
package TreeDemo;
import java.util.*;
public class BinaryTreeTest {public static class TreeNode {int val;TreeNode left;TreeNode right;public TreeNode(int val) {this.val = val;}}/*** 下面的几种方法分别是* 前中后序的递归遍历(又称深度优先搜索)* 前中后序的迭代法遍历(用栈去模拟)* 层序遍历(广度优先搜索 --> 用队列模拟)*///前序遍历的递归法List<Integer> list1 = new ArrayList<>();public List<Integer> preOrder(TreeNode root) {if (root == null) {return list1;}//进入递归环节list1.add(root.val);preOrder(root.left);preOrder(root.right);return list1;}//前序遍历的非递归写法(迭代法)private Stack<TreeNode> stack1 = new Stack<>();private List<Integer> list2 = new ArrayList<>();public List<Integer> preOrderUseWhile(TreeNode root) {if (root == null) {return list2;}stack1.push(root);while (!stack1.isEmpty()) {TreeNode node = stack1.pop();list2.add(node.val);if (node.right != null) {stack1.push(root.right);}if (node.left != null) {stack1.push(root.left);}}return list2;}//中序遍历的递归法private List<Integer> list3 = new ArrayList<>();public List<Integer> inOrder(TreeNode root) {if (root == null) {return list3;}inOrder(root.left);list3.add(root.val);inOrder(root.right);return list3;}//中序遍历的迭代法private List<Integer> list4 = new ArrayList<>();private Stack<TreeNode> stack2 = new Stack<>();public List<Integer> inOrderUseWhile(TreeNode root) {if (root == null) {return list4;}TreeNode cur = root;//这里的逻辑判断要重视一下啊,这个条件的逆命题是栈为空同时指针也为空...while (!stack2.isEmpty() || cur != null) {if (cur != null) {stack2.push(cur);cur = cur.left;} else {TreeNode node = stack2.pop();list4.add(node.val);cur = node.right;}}return list4;}//后序遍历的递归法private List<Integer> posOrderList = new ArrayList<>();public List<Integer> posOrder(TreeNode root) {if (root == null) {return posOrderList;}posOrder(root.left);posOrder(root.right);posOrderList.add(root.val);return posOrderList;}//后续遍历的迭代法private List<Integer> posOrderlist = new ArrayList<>();private Stack<TreeNode> posOrderStack = new Stack<>();public List<Integer> posOrderUseWhile(TreeNode root) {if (root == null) {return posOrderlist;}posOrderStack.push(root);while (!posOrderStack.isEmpty()) {TreeNode node = posOrderStack.pop();posOrderlist.add(node.val);if (node.left != null) {posOrderStack.push(node.left);}if (node.right != null) {posOrderStack.push(node.right);}}//反转顺序表int size = posOrderlist.size();int left = 0;int right = size - 1;while (left <= right) {Integer temp = posOrderlist.get(left);posOrderlist.set(left, posOrderlist.get(right));posOrderlist.set(right, temp);left++;right--;}return posOrderlist;}/*** 二叉树的层序遍历,运用的是队列的思想* 核心逻辑就是利用队列的大小来定向的控制移动元素的数量从而达到层序遍历的效果* 也就是我们图论中的广度优先搜索...*/public List<List<Integer>> levelList = new ArrayList<>();public Queue<TreeNode> queue = new LinkedList<>();public List<List<Integer>> levelOrder(TreeNode root) {if(root == null){return levelList;}queue.offer(root);while(!queue.isEmpty()){int size = queue.size();List<Integer> tempList = new ArrayList<>();while(size-- != 0){TreeNode node = queue.poll();tempList.add(node.val);if(node.left != null){queue.offer(node.left);}if(node.right != null){queue.offer(node.right);}}levelList.add(tempList);}return levelList;}
}