前序遍历: 遍历顺序是根节点,左子树,右子树
中序遍历的顺序是左子树、根节点、右子树
后序遍历的顺序是左子树、右子树、根节点。
/*** @Description: 递归 实现 二叉树的 先中后序* @Author: wule* @Date: 2024/7/1 13:44*/ public class Erchashu {/*** 定义树*/class TreeNode {public int val;public TreeNode left;public TreeNode right;public TreeNode() {this.val = val;}}// 前序遍历public void preOrder(TreeNode root) {if (root == null) {return;}preOrder(root.left);preOrder(root.right);}// 中序遍历public void inOrder(TreeNode root) {if (root == null) {return;}inOrder(root.left);inOrder(root.right);}// 后序遍历public void backOrder(TreeNode root) {if (root == null) {return;}backOrder(root.left);backOrder(root.right);}}