面试必考01——二叉树的前中后遍历

本文详细介绍了二叉树的先序、中序、后序遍历算法,包括递归和非递归实现方式,并提供了完整的Java代码示例。通过本文,读者可以深入理解二叉树遍历的不同方法及其应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现二叉树的先序、中序、后序遍历,包括递归方式和非递归方式

import java.util.Stack;

//二叉树的前序、中序、后序遍历(递归和非递归方法)
//二叉树的层次遍历(只有非递归的方法)

public class T01PreInPostOrder {

    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int data) {
            this.value = data;
        }
    }

    public static void preOrder(Node head) {
        if (head == null) {
            return;
        }
        System.out.print(head.value + " ");
        preOrder(head.left);
        preOrder(head.right);
    }

    public static void inOrder(Node head) {
        if (head == null) {
            return;
        }
        inOrder(head.left);
        System.out.print(head.value + " ");
        inOrder(head.right);
    }

    public static void postOrder(Node head) {
        if (head == null) {
            return;
        }
        postOrder(head.left);
        postOrder(head.right);
        System.out.print(head.value + " ");
    }

    /**
     * 先序遍历
     * 往栈中压入根结点
     * 弹出栈中一个结点并打印
     * 压入刚弹出结点的右结点和左结点
     * 弹出栈中一个结点并打印
     */
    public static void preOrderNR(Node head) {
        if (head != null) {
            Stack<Node> stack = new Stack<>();
            stack.push(head);
            Node res = null;
            while (!stack.isEmpty()) {
                res = stack.pop();
                System.out.print(res.value + " ");
                if (res.right != null) {
                    stack.push(res.right);
                }
                if (res.left != null) {
                    stack.push(res.left);
                }
            }
        }
    }

    /**
     * 中序遍历
     * 当前结点不为空时,压入当前结点,当前结点指针向它左结点移动
     * 当前结点为空、栈不为空时,弹出栈结点并打印,当前结点指针向栈结点的右结点移动
     */
    public static void inOrderNR(Node head) {
        if (head != null) {
            Stack<Node> stack = new Stack<>();
            Node cur = head;
            while (!stack.isEmpty() || cur != null) {
                if (cur != null) {
                    stack.push(cur);
                    cur = cur.left;
                } else {
                    cur = stack.pop();
                    System.out.print(cur.value + " ");
                    cur = cur.right;
                }
            }
        }
    }

    /**
     * 后序遍历
     * 由前面的先序遍历,中左右,改为中右左,然后放入栈中逆序,得到左右中,即后序遍历
     */
    public static void postOrderNR(Node head) {
        if (head != null) {
            Stack<Node> s1 = new Stack<>();
            Stack<Node> s2 = new Stack<>();
            s1.push(head);
            Node res = null;
            while (!s1.isEmpty()) {
                res = s1.pop();
                s2.push(res);
                if (res.left != null) {
                    s1.push(res.left);
                }
                if (res.right != null) {
                    s1.push(res.right);
                }
            }
            while (!s2.isEmpty()) {
                System.out.print(s2.pop().value + " ");
            }
        }
    }

    public static void main(String[] args) {
        Node head = new Node(5);
        head.left = new Node(3);
        head.right = new Node(8);
        head.left.left = new Node(2);
        head.left.right = new Node(4);
        head.left.left.left = new Node(1);
        head.right.left = new Node(7);
        head.right.left.left = new Node(6);
        head.right.right = new Node(10);
        head.right.right.left = new Node(9);
        head.right.right.right = new Node(11);

        // recursive
        System.out.println("==============recursive==============");
        System.out.print("pre-order: ");
        preOrder(head);
        System.out.println();
        System.out.print("in-order: ");
        inOrder(head);
        System.out.println();
        System.out.print("post-order: ");
        postOrder(head);
        System.out.println();

        // unrecursive
        System.out.println("============unrecursive=============");
        System.out.print("pre-order: ");
        preOrderNR(head);
        System.out.println();
        System.out.print("in-order: ");
        inOrderNR(head);
        System.out.println();
        System.out.print("post-order: ");
        postOrderNR(head);
        System.out.println();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值