LeetCode:102. 二叉树的层序遍历(java)

目录

题目描述:

代码:

第一种:

第二种:

TreeNode:

LinkedListNode:


题目描述:

给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。

示例 1:

复制代码
输入:root = [3,9,20,null,null,15,7]
输出:[[3],[9,20],[15,7]]

示例 2:

复制代码
输入:root = [1]
输出:[[1]]

示例 3:

复制代码
输入:root = []
输出:[]

代码:

第一种:

复制代码
    LinkedListQueue<TreeNode> queue = new LinkedListQueue<TreeNode>();
        queue.offer(root);
        int c1=1;//当前的节点数
        while(!queue.isEmpty()){
            int c2=0;
            for(int i=0;i<c1;i++){
                TreeNode n=queue.poll();
                System.out.print(n+" ");
                if(n.left!=null){
                    queue.offer(n.left);
                    c2++;
                }
                if(n.right!=null){
                    queue.offer(n.right);
                    c2++;
                }
            }
            System.out.println();
            c1=c2;

        }

第二种:

复制代码
 public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer> > res = new ArrayList<>();
        if(root==null) return res;
        LinkedListQueue<TreeNode> queue = new LinkedListQueue<TreeNode>();
        queue.offer(root);
        int c1=1;//当前的节点数
        while(!queue.isEmpty()){
            List<Integer> level=new ArrayList<>();
            int c2=0;
            for(int i=0;i<c1;i++){
                TreeNode n=queue.poll();
                level.add(n.val);
                if(n.left!=null){
                    queue.offer(n.left);
                    c2++;
                }
                if(n.right!=null){
                    queue.offer(n.right);
                    c2++;
                }
            }
            res.add(level);
            c1=c2;

        }
        return res;

    }

TreeNode:

复制代码
package TreeNode;

public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;
    public TreeNode(int val)
    {
        this.val = val;
    }
    public TreeNode( TreeNode left,int val, TreeNode right){
        this.val = val;
        this.left = left;
        this.right = right;
    }
    public String toString(){
        return String.valueOf(val);
    }

}

LinkedListNode:

复制代码
public class LinkedListQueue <E> implements Queue<E>, Iterable<E>{//单向环形哨兵链表
    //节点类
    private static class Node<E>{
        E value;
        Node<E> next;
        Node(E value, Node<E> next){
            this.value=value;
            this.next=next;
        }
    }
    Node<E> head=new Node<>(null,null);
    Node<E> tail=head;//tail指向头
    private int size=0;//节点数
    private int capacity=Integer.MIN_VALUE;//容量
    public LinkedListQueue(){
        tail.next=head;
    }
    public LinkedListQueue(int capacity){
        this.capacity=capacity;
        tail.next=head;
    }
    //从队尾插入
    @Override
    public boolean offer(E value) {
        if(isFull()){
            return false;
        }
       Node<E> added= new Node<>(value,head);//added新节点尾部指向头
       tail.next=added;//原来的尾部指向新节点
       tail=added;//尾部指向新节点
        size++;
        return true;
    }
    //获取队列的第一个值,并移除

    @Override
    public E poll() {
        if(isEmpty()){
            return null;
        }
        E value=head.next.value;
        Node<E> first=head.next;
        head.next=first.next;
        size--;
        if(first==tail){
            tail=head;
        }
        return value;
    }
    //从对头获取值,但不移除

    @Override
    public E peek() {
        if(isEmpty()){
            return null;
        }
        return head.next.value;
    }

    @Override
    public boolean isEmpty() {
       return head==tail;
    }

    @Override
    public boolean isFull() {
        return size==capacity;
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>(){
           Node<E> p=head.next;
           public boolean hasNext() {//p==head退出
               return p!=head;
           }
           public E next(){
               E value=p.value;
               p=p.next;
               return value;
           }
        };
    }

}
相关推荐
大怪v15 小时前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
惯导马工17 小时前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
骑自行车的码农19 小时前
【React用到的一些算法】游标和栈
算法·react.js
博笙困了19 小时前
AcWing学习——双指针算法
c++·算法
moonlifesudo19 小时前
322:零钱兑换(三种方法)
算法
NAGNIP2 天前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
美团技术团队2 天前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
Fanxt_Ja2 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法
茉莉玫瑰花茶2 天前
算法 --- 字符串
算法