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

}
相关推荐
能工智人小辰17 分钟前
Codeforces Round 509 (Div. 2) C. Coffee Break
c语言·c++·算法
kingmax5421200818 分钟前
CCF GESP202503 Grade4-B4263 [GESP202503 四级] 荒地开垦
数据结构·算法
岁忧23 分钟前
LeetCode 高频 SQL 50 题(基础版)之 【高级字符串函数 / 正则表达式 / 子句】· 上
sql·算法·leetcode
eachin_z1 小时前
力扣刷题(第四十九天)
算法·leetcode·职场和发展
闻缺陷则喜何志丹1 小时前
【强连通分量 缩点 拓扑排序】P3387 【模板】缩点|普及+
c++·算法·拓扑排序·洛谷·强连通分量·缩点
钮钴禄·爱因斯晨1 小时前
Java 面向对象进阶之多态:从概念到实践的深度解析
java·开发语言·数据结构
机器学习之心2 小时前
机器学习用于算法交易(Matlab实现)
算法·机器学习·matlab
AL流云。2 小时前
【优选算法】C++滑动窗口
数据结构·c++·算法
qq_429879673 小时前
省略号和可变参数模板
开发语言·c++·算法
飞川撸码4 小时前
【LeetCode 热题100】网格路径类 DP 系列题:不同路径 & 最小路径和(力扣62 / 64 )(Go语言版)
算法·leetcode·golang·动态规划