深入理解二叉树遍历:递归与迭代实现


介绍

在数据结构中,二叉树是一种重要且常见的数据结构,它的遍历方式包括先序、中序和后序三种。本文将通过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语言实现二叉树的先序、中序和后序遍历,包括递归和迭代两种实现方式。递归版本简洁明了,迭代版本则展示了如何使用栈来模拟递归过程,确保了遍历的顺序性和正确性。选择适合自己项目需求的遍历方式可以有效提高代码的效率和可读性。

希望本文对你理解二叉树的遍历方式有所帮助!


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值