介绍
在数据结构中,二叉树是一种重要且常见的数据结构,它的遍历方式包括先序、中序和后序三种。本文将通过Java语言,详细讲解如何实现这三种遍历方式的递归和迭代版本。
二叉树定义
首先,我们定义二叉树的节点类 TreeNode
:
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
先序遍历 (Preorder Traversal)
先序遍历的顺序是:根节点 -> 左子树 -> 右子树。
递归实现
public class BinaryTreeTraversal {
// 先序遍历递归实现
public void preorderRecursive(TreeNode root) {
if (root != null) {
System.out.print(root.val + " ");
preorderRecursive(root.left);
preorderRecursive(root.right);
}
}
}
迭代实现
import java.util.Stack;
public class BinaryTreeTraversal {
// 先序遍历迭代实现
public void preorderIterative(TreeNode root) {
if (root == null) return;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
System.out.print(node.val + " ");
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}
}
}
中序遍历 (Inorder Traversal)
中序遍历的顺序是:左子树 -> 根节点 -> 右子树。
递归实现
public class BinaryTreeTraversal {
// 中序遍历递归实现
public void inorderRecursive(TreeNode root) {
if (root != null) {
inorderRecursive(root.left);
System.out.print(root.val + " ");
inorderRecursive(root.right);
}
}
}
迭代实现
import java.util.Stack;
public class BinaryTreeTraversal {
// 中序遍历迭代实现
public void inorderIterative(TreeNode root) {
if (root == null) return;
Stack<TreeNode> stack = new Stack<>();
TreeNode current = root;
while (current != null || !stack.isEmpty()) {
while (current != null) {
stack.push(current);
current = current.left;
}
current = stack.pop();
System.out.print(current.val + " ");
current = current.right;
}
}
}
后序遍历 (Postorder Traversal)
后序遍历的顺序是:左子树 -> 右子树 -> 根节点。
递归实现
public class BinaryTreeTraversal {
// 后序遍历递归实现
public void postorderRecursive(TreeNode root) {
if (root != null) {
postorderRecursive(root.left);
postorderRecursive(root.right);
System.out.print(root.val + " ");
}
}
}
迭代实现
后序遍历的迭代实现方法有多种,下面演示两种常见的方法:
方法一:使用两个栈
import java.util.Stack;
public class BinaryTreeTraversal {
// 后序遍历迭代实现方法一:使用两个栈
public void postorderIterative(TreeNode root) {
if (root == null) return;
Stack<TreeNode> stack1 = new Stack<>();
Stack<TreeNode> stack2 = new Stack<>();
stack1.push(root);
while (!stack1.isEmpty()) {
TreeNode node = stack1.pop();
stack2.push(node);
if (node.left != null) {
stack1.push(node.left);
}
if (node.right != null) {
stack1.push(node.right);
}
}
while (!stack2.isEmpty()) {
System.out.print(stack2.pop().val + " ");
}
}
}
方法二:单栈解法,反转结果
import java.util.Stack;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class BinaryTreeTraversal {
// 后序遍历迭代实现方法二:单栈解法,反转结果
public List<Integer> postorderIterative2(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.left != null) {
stack.push(node.left);
}
if (node.right != null) {
stack.push(node.right);
}
}
Collections.reverse(result);
return result;
}
}
总结
本文详细介绍了如何使用Java语言实现二叉树的先序、中序和后序遍历,包括递归和迭代两种实现方式。递归版本简洁明了,迭代版本则展示了如何使用栈来模拟递归过程,确保了遍历的顺序性和正确性。选择适合自己项目需求的遍历方式可以有效提高代码的效率和可读性。
希望本文对你理解二叉树的遍历方式有所帮助!