栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

二叉树遍历(递归和非递归)前序、中序、后序遍历

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

二叉树遍历(递归和非递归)前序、中序、后序遍历

二叉树前序遍历(递归)
    public static void recursionBefore(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.getValue() + "->");
        recursionBefore(root.getLeft());
        recursionBefore(root.getRight());
    }
二叉树前序遍历(非递归)
    public static List beforeTree(TreeNode root){
        if (root == null) {
            return null;
        }
        List result = new ArrayList<>();
        Stack stack = new Stack<>();
        TreeNode node = root;
        while (node != null || !stack.isEmpty()) {
            if (node != null) {
                if (node != null) {
                    result.add(node.getValue());
                }
                stack.push(node);
                node = node.getLeft();

            } else {
                TreeNode tem = stack.pop();
                node = tem.getRight();
            }
        }
        return result;
    }
二叉树中序遍历(递归)
    public static void recursionMiddle(TreeNode root) {
        if (root == null) {
            return;
        }
        recursionMiddle(root.getLeft());
        System.out.print(root.getValue() + "->");
        recursionMiddle(root.getRight());
    }
二叉树中序遍历(非递归)
    public static List middleTree(TreeNode root){
        if (root == null) {
            return null;
        }
        List result = new ArrayList<>();
        Stack stack = new Stack<>();
        TreeNode node = root;
        while (node != null || !stack.isEmpty()) {
            if (node != null) {
                stack.push(node);
                node = node.getLeft();
            } else {
                TreeNode tem = stack.pop();
                if (tem != null) {
                    result.add(tem.getValue());
                }
                node = tem.getRight();
            }
        }
        return result;
    }
二叉树后序遍历(递归)
    public static void recursionAfter(TreeNode root) {
        if (root == null) {
            return;
        }
        recursionAfter(root.getLeft());
        recursionAfter(root.getRight());
        System.out.print(root.getValue() + "->");
    }
二叉树后序遍历(非递归)
    public static void afterTree(TreeNode root) {
        TreeNode current = root;
        //把s1,linkedList作为栈使用
        linkedList s1 = new linkedList();
        //把S2,把linkedList作为栈使用
        linkedList s2 = new linkedList();
        while (current != null || !s1.isEmpty()) {
            while (current != null) {
                s1.addFirst(current);
                s2.addFirst(current);
                current = current.getRight();
            }
            if (!s1.isEmpty()) {
                current = s1.removeFirst();
                current = current.getLeft();
            }
        }
        while (!s2.isEmpty()) {
            System.out.print(s2.removeFirst().getValue() + " -> ");
        }
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/305911.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号