1、二叉树的遍历:

  1)、深度优先遍历:遍历顺序如下
    1.前序遍历:根节点、左子树、右子树
    2.中序遍历:左子树、根节点、右子树
    3.后序遍历:左子树、右子树、根节点
  
  2)、广度优先遍历:
    1.层序遍历:

2、代码如下:

package com.algorithm.tiger.tree;

import com.algorithm.tiger.json.JsonUtils;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
* @description: 遍历二叉树
* @author: tiger
* @create: 2020-07-18 10:19
*/
public class MyTree {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>(
Arrays.asList(3, 2, 9, null, null, 10, null, null, 8, null, 4));
TreeNode node = createBinaryTree(list);
// System.out.println(JsonUtils.toJson(node));
// preOrderTraveral(node);
// System.out.println("==================");
// preOrderTraveralWithStack(node);
levelOrderTraversal(node);
}

/**
* 构建二叉树
*
* @param list
* @return
*/
public static TreeNode createBinaryTree(LinkedList<Integer> list) {
//3, 2, 9, null, null, 10, null, null, 8, null, 4
TreeNode node = null;
//返回条件
if (CollectionUtils.isEmpty(list)) return null;

Integer data = list.removeFirst();
//平衡左右子树
if (null != data) {
node = new TreeNode(data);
node.leftChild = createBinaryTree(list);
node.rightChild = createBinaryTree(list);
}
return node;
}

/**
* 前序遍历:根节点、左子树、右子树
*
* @param node
*/
public static void preOrderTraveral(TreeNode node) {
if (null == node) return;
System.out.println(node.data);
preOrderTraveral(node.leftChild);
preOrderTraveral(node.rightChild);
}

/**
* 中序遍历:左子树、根节点、右子树
*
* @param node
*/
public static void inOrderTraveral(TreeNode node) {
if (null == node) return;
preOrderTraveral(node.leftChild);
System.out.println(node.data);
preOrderTraveral(node.rightChild);
}

/**
* 后序遍历:左子树、右子树、根节点
*
* @param node
*/
public static void postOrderTraveral(TreeNode node) {
if (null == node) return;
preOrderTraveral(node.leftChild);
preOrderTraveral(node.rightChild);
System.out.println(node.data);
}

/**
* 利用栈进行前序遍历
*
* @param root
*/
public static void preOrderTraveralWithStack(TreeNode root) {
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode treeNode = root;
//迭代访问节点的左孩子,并入栈
while (treeNode != null || !stack.isEmpty()) {
System.out.println(treeNode.data);
stack.push(treeNode);
treeNode = treeNode.leftChild;
//如果节点没有左孩子,则弹出栈顶节点,访问节点右孩子
if (!stack.isEmpty()) {
treeNode = stack.pop();
treeNode = treeNode.rightChild;
}
}
}

/**
* 利用队列来实现【二叉树层序遍历】
*
* @param root
*/
public static void levelOrderTraversal(TreeNode root) {

Queue<TreeNode> queue = new LinkedList<>();
//首先不管3721根节点入队
queue.offer(root);
while (!queue.isEmpty()) {
//父节点出队
TreeNode node = queue.poll();
//打印输出第一层树
System.out.println(node.data);
//让父节点出队的左右孩子节点入队
if (node.leftChild != null) {
queue.offer(node.leftChild);
}
if (node.rightChild != null) {
queue.offer(node.rightChild);
}
}

}

/**
* 树节点
*/
private static class TreeNode {
Integer data;
TreeNode leftChild;
TreeNode rightChild;

public TreeNode(Integer data) {
this.data = data;
}

public Integer getData() {
// return data == null ? -1 : data;
return data;
}

public void setData(Integer data) {
this.data = data;
}

public TreeNode getLeftChild() {
return leftChild;
}

public void setLeftChild(TreeNode leftChild) {
this.leftChild = leftChild;
}

public TreeNode getRightChild() {
return rightChild;
}

public void setRightChild(TreeNode rightChild) {
this.rightChild = rightChild;
}

@Override
public String toString() {
return "TreeNode{" +
"data=" + data +
", leftChild=" + leftChild +
", rightChild=" + rightChild +
'}';
}
}
}