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;
           }
        };
    }

}
相关推荐
LYFlied1 天前
【每日算法】LeetCode 153. 寻找旋转排序数组中的最小值
数据结构·算法·leetcode·面试·职场和发展
唐装鼠1 天前
rust自动调用Deref(deepseek)
开发语言·算法·rust
ytttr8731 天前
MATLAB基于LDA的人脸识别算法实现(ORL数据库)
数据库·算法·matlab
jianfeng_zhu1 天前
整数数组匹配
数据结构·c++·算法
yueqingll1 天前
032、数据结构之代码时间复杂度和空间复杂度的判断:从入门到实战
数据结构
smj2302_796826521 天前
解决leetcode第3782题交替删除操作后最后剩下的整数
python·算法·leetcode
LYFlied1 天前
【每日算法】LeetCode 136. 只出现一次的数字
前端·算法·leetcode·面试·职场和发展
唯唯qwe-1 天前
Day23:动态规划 | 爬楼梯,不同路径,拆分
算法·leetcode·动态规划
做科研的周师兄1 天前
中国土壤有机质数据集
人工智能·算法·机器学习·分类·数据挖掘
来深圳1 天前
leetcode 739. 每日温度
java·算法·leetcode