回顾:树的几种迭代遍历方式

本文详细介绍了树的四种遍历方式:中序遍历、前序遍历、后序遍历和层次遍历,并提供了相应的测试代码。

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

PS:文章最后附上完整代码

一、中序遍历

List<Integer> inOrder(BTree root) {
        List<Integer> result = new ArrayList<>();

        Stack<BTree> stack = new Stack<>();
        BTree node;
        while (null != root || !stack.empty()) {
            while (null != root) {
                stack.push(root);
                root = root.left;
            }
            node = stack.pop();
            result.add(node.val);
            if (null != node.right) {
                root = node.right;
            }
        }

        return result;
    }

二、前序遍历

List<Integer> preOrder(BTree root) {
        List<Integer> result = new ArrayList<>();

        Stack<BTree> stack = new Stack<>();
        BTree node;
        while (null != root || !stack.empty()) {
            while (null != root) {
                result.add(root.val);
                stack.push(root);
                root = root.left;
            }
            node = stack.pop();
            if (null != node.right) {
                root = node.right;
            }
        }

        return result;
    }

三、后序遍历

List<Integer> postOrder(BTree root) {
        List<Integer> result = new ArrayList<>();

        Stack<BTree> stack = new Stack<>();
        BTree node;
        while (null != root || !stack.empty()) {
            while (null != root) {
                result.add(0, root.val);
                stack.push(root);
                root = root.right;
            }
            node = stack.pop();
            if (null != node.left) {
                root = node.left;
            }
        }

        return result;
    }

四、层次遍历

List<Integer> layerOrder(BTree root) {
        List<Integer> result = new ArrayList<>();
        if (null == root) {
            return result;
        }

        Queue<BTree> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            BTree node = queue.poll();
            if (null != node.left) {
                queue.offer(node.left);
            }
            if (null != node.right) {
                queue.offer(node.right);
            }
            result.add(node.val);
        }

        return result;
    }

测试代码:

import java.util.*;

/**
 * Copyright © 2018 Chris. All rights reserved.
 *
 * @author Chris
 */

public class Solution {

    static class BTree {
        int val;
        BTree left, right;
        BTree(int v) {val = v;}

        @Override
        public String toString() {
            return val + "";
        }
    }

    /**
     * 层次遍历
     *
     * @param root 根结点
     * @return 遍历结果
     */
    List<Integer> layerOrder(BTree root) {
        List<Integer> result = new ArrayList<>();
        if (null == root) {
            return result;
        }

        Queue<BTree> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            BTree node = queue.poll();
            if (null != node.left) {
                queue.offer(node.left);
            }
            if (null != node.right) {
                queue.offer(node.right);
            }
            result.add(node.val);
        }

        return result;
    }
    /**
     * 后序遍历
     *
     * @param root 根结点
     * @return 遍历结果
     */

    List<Integer> postOrder(BTree root) {
        List<Integer> result = new ArrayList<>();

        Stack<BTree> stack = new Stack<>();
        BTree node;
        while (null != root || !stack.empty()) {
            while (null != root) {
                result.add(0, root.val);
                stack.push(root);
                root = root.right;
            }
            node = stack.pop();
            if (null != node.left) {
                root = node.left;
            }
        }

        return result;
    }

    /**
     * 前序遍历
     *
     * @param root 根结点
     * @return 遍历结果
     */
    List<Integer> preOrder(BTree root) {
        List<Integer> result = new ArrayList<>();

        Stack<BTree> stack = new Stack<>();
        BTree node;
        while (null != root || !stack.empty()) {
            while (null != root) {
                result.add(root.val);
                stack.push(root);
                root = root.left;
            }
            node = stack.pop();
            if (null != node.right) {
                root = node.right;
            }
        }

        return result;
    }

    /**
     * 中序遍历
     *
     * @param root 根结点
     * @return 遍历结果
     */
    List<Integer> inOrder(BTree root) {
        List<Integer> result = new ArrayList<>();

        Stack<BTree> stack = new Stack<>();
        BTree node;
        while (null != root || !stack.empty()) {
            while (null != root) {
                stack.push(root);
                root = root.left;
            }
            node = stack.pop();
            result.add(node.val);
            if (null != node.right) {
                root = node.right;
            }
        }

        return result;
    }

    public static void main(String args[]) {

        Solution s = new Solution();
        BTree root = new BTree(1);
        BTree left = new BTree(2);
        BTree right = new BTree(3);
        left.left = new BTree(4);
        left.right = new BTree(5);
        right.left = new BTree(6);
        right.right = new BTree(7);
        root.left = left;
        root.right  = right;
        System.out.println(s.inOrder(root));
        System.out.println(s.preOrder(root));
        System.out.println(s.postOrder(root));
        System.out.println(s.layerOrder(root));
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值