树的遍历主要有四种基本方式:前序遍历、中序遍历、后序遍历和层次遍历。以下是它们的Java实现示例:
1. 二叉树节点类定义
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
this.left = null;
this.right = null;
}
}
2. 递归遍历实现
public class TreeTraversal {
// 前序遍历:根 -> 左 -> 右
public void preorder(TreeNode root) {
if (root == null) return;
System.out.print(root.val + " ");
preorder(root.left);
preorder(root.right);
}
// 中序遍历:左 -> 根 -> 右
public void inorder(TreeNode root) {
if (root == null) return;
inorder(root.left);
System.out.print(root.val + " ");
inorder(root.right);
}
// 后序遍历:左 -> 右 -> 根
public void postorder(TreeNode root) {
if (root == null) return;
postorder(root.left);
postorder(root.right);
System.out.print(root.val + " ");
}
}
3. 迭代遍历实现
import java.util.*;
public class IterativeTraversal {
// 迭代前序遍历
public List<Integer> preorderIterative(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
result.add(node.val);
if (node.right != null) stack.push(node.right);
if (node.left != null) stack.push(node.left);
}
return result;
}
// 迭代中序遍历
public List<Integer> inorderIterative(TreeNode root) {
List<Integer> result = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode curr = root;
while (curr != null || !stack.isEmpty()) {
while (curr != null) {
stack.push(curr);
curr = curr.left;
}
curr = stack.pop();
result.add(curr.val);
curr = curr.right;
}
return result;
}
// 迭代后序遍历
public List<Integer> postorderIterative(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
result.add(0, node.val); // 添加到开头实现逆序
if (node.left != null) stack.push(node.left);
if (node.right != null) stack.push(node.right);
}
return result;
}
// 层次遍历(广度优先)
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
if (root == null) return result;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int levelSize = queue.size();
List<Integer> level = new ArrayList<>();
for (int i = 0; i < levelSize; i++) {
TreeNode node = queue.poll();
level.add(node.val);
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
result.add(level);
}
return result;
}
}
4. 使用示例
public class Main {
public static void main(String[] args) {
// 构建示例二叉树
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
TreeTraversal traversal = new TreeTraversal();
System.out.print("前序遍历: ");
traversal.preorder(root); // 输出: 1 2 4 5 3
System.out.print("\n中序遍历: ");
traversal.inorder(root); // 输出: 4 2 5 1 3
System.out.print("\n后序遍历: ");
traversal.postorder(root); // 输出: 4 5 2 3 1
}
}
总结
- 前序遍历:先访问根节点,然后左子树,最后右子树
- 中序遍历:先访问左子树,然后根节点,最后右子树
- 后序遍历:先访问左子树,然后右子树,最后根节点
- 层次遍历:按层次从上到下,每层从左到右访问
这些遍历方法在树的各种操作中都有重要应用,如表达式求值、序列化/反序列化二叉树等。
469

被折叠的 条评论
为什么被折叠?



