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

}
相关推荐
sweet丶5 分钟前
iOS MMKV原理整理总结:比UserDefaults快100倍的存储方案是如何炼成的?
算法·架构
云里雾里!1 小时前
力扣 209. 长度最小的子数组:滑动窗口解法完整解析
数据结构·算法·leetcode
CoderYanger2 小时前
递归、搜索与回溯-穷举vs暴搜vs深搜vs回溯vs剪枝:12.全排列
java·算法·leetcode·机器学习·深度优先·剪枝·1024程序员节
憨憨崽&2 小时前
进击大厂:程序员必须修炼的算法“内功”与思维体系
开发语言·数据结构·算法·链表·贪心算法·线性回归·动态规划
chem41113 小时前
C 语言 函数指针和函数指针数组
c语言·数据结构·算法
liu****4 小时前
八.函数递归
c语言·开发语言·数据结构·c++·算法
客梦4 小时前
数据结构-树结构
数据结构·笔记
CM莫问4 小时前
详解机器学习经典模型(原理及应用)——岭回归
人工智能·python·算法·机器学习·回归
DuHz4 小时前
论文阅读——Edge Impulse:面向微型机器学习的MLOps平台
论文阅读·人工智能·物联网·算法·机器学习·edge·边缘计算
梦想的旅途24 小时前
基于雪花算法(Snowflake)的 Go 语言唯一 ID 生成与并发安全实现
算法·安全·golang