二叉树的层级遍历

文章介绍了在Java中实现的几种树结构的层次遍历方法,包括简单的levelOrder、levelOrderDesc、zigzagLevelOrder以及针对n叉树的nLevelOrder。这些方法使用了队列和递归,展示了不同场景下的层次遍历逻辑。
摘要由CSDN通过智能技术生成

simpleLevelOrder广度优先遍历

levelOrder返回每层的列表

levelOrderDESC倒序返回每层的列表

zigzagLevelOrder交叉逆序返回每层列表

nLevelOrder返回n叉树的每层列表



import java.util.*;

public class LevelOrder {
    public static void main(String[] args) {
        Node root = new Node(3);
        Node n2 = new Node(9);
        Node n3 = new Node(20);
        Node n4 = new Node(15);
        Node n5 = new Node(7);
        root.left = n2;root.right=n3;n3.left=n4;n3.right=n5;
        System.out.println(simpleLevelOrder(root));
        System.out.println(levelOrder(root));
        System.out.println(levelOrderDesc(root));
        System.out.println(zigzagLevelOrder(root));

        NNode nroot = new NNode(1);
        NNode nn2 = new NNode(3);
        NNode nn3 = new NNode(2);
        NNode nn4 = new NNode(4);
        NNode nn5 = new NNode(5);
        NNode nn6 = new NNode(6);
        nroot.children= new ArrayList<>(){{add(nn2);add(nn3);add(nn4);}};
        nn2.children=new ArrayList<>(){{add(nn5);add(nn6);}};
        System.out.println(nLevelOrder(nroot));
    }
    public static List<List<Integer>> nLevelOrder(NNode root){
        List<List<Integer>> res = new LinkedList<>();
        Queue queue = new LinkedList<NNode>();
        queue.offer(root);
        while (queue.size()>0){
            int size = queue.size();
            List<Integer> level = new LinkedList<>();
            for (;size>0;size--){
                NNode t = (NNode) queue.remove();
                List<NNode> children= t.children;
                if (children!=null){
                children.stream().forEach(i->{
                    queue.offer(i);
                });
                }
                level.add(t.value);
            }
            res.add(level);
        }
        return res;
    }
    public static List<List<Integer>> zigzagLevelOrder(Node root){
        if (root==null){
            return new ArrayList<>();
        }
        List<List<Integer>> res = new ArrayList<>();
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        boolean desc = false;
        while (queue.size()>0){
            int size = queue.size();
            List<Integer> level = new ArrayList<>();
            for (;size>0;size--){
                Node t = queue.poll();
                Node left = t.left;
                Node right = t.right;
                if (left!=null){
                    queue.add(left);
                }
                if (right!=null){
                    queue.add(right);
                }
                if (desc) {
                    level.add(0,t.value);
                }else {
                    level.add(t.value);
                }
            }
            res.add(level);
            desc=!desc;
        }
        return res;
    }
    public static List<List<Integer>> levelOrderDesc(Node root){
        if (root==null){
            return new ArrayList<>();
        }
        List<List<Integer>> res = new ArrayList<>();
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        while (queue.size()>0){
            int size = queue.size();
            List<Integer> level = new ArrayList<>();
            for (;size>0;size--){
                Node t = queue.poll();
                Node left = t.left;
                Node right = t.right;
                if (left!=null){
                    queue.add(left);
                }
                if (right!=null){
                    queue.add(right);
                }
                level.add(t.value);
            }
            res.add(0,level);
        }
        return res;
    }
    public static List<List<Integer>> levelOrder(Node root){
        if (root==null){
            return new ArrayList<>();
        }
        List<List<Integer>> res = new ArrayList<>();
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        while (queue.size()>0){
            int size = queue.size();
            List<Integer> level = new ArrayList<>();
            for (;size>0;size--){
                Node t = queue.poll();
                Node left = t.left;
                Node right = t.right;
                if (left!=null){
                    queue.add(left);
                }
                if (right!=null){
                    queue.add(right);
                }
                level.add(t.value);
            }
            res.add(level);
        }
        return res;
    }

    public static List<Integer> simpleLevelOrder(Node root){
        if (root==null){
            return new ArrayList<>();
        }
        List<Integer> res = new ArrayList<>();
        LinkedList<Node> queue = new LinkedList<>();
        queue.add(root);
        while (queue.size()>0){
            int size = queue.size();
            for (;size>0;size--){
                Node t =queue.remove();
                Node left =t.left;
                Node right = t.right;
                if (left!=null){
                    queue.add(left);
                }
                if (right!=null){
                    queue.add(right);
                }
                res.add(t.value);
            }

        }
        return res;
    }




}
class Node{
    int value;
    Node left;
    Node right;

    public Node() {
    }

    public Node(int value) {
        this.value = value;
    }
}
class NNode{
    int value;
    List<NNode> children;

    public NNode(int value) {
        this.value = value;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值