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

}
相关推荐
寂静山林14 小时前
UVa 1366 Martian Mining
算法
陌路2015 小时前
S12 简单排序算法--冒泡 选择 直接插入 希尔排序
数据结构·算法·排序算法
雾岛—听风15 小时前
P1012 [NOIP 1998 提高组] 拼数
算法
papership16 小时前
【入门级-算法-5、数值处理算法:高精度的乘法】
数据结构·算法
earthzhang202116 小时前
【1039】判断数正负
开发语言·数据结构·c++·算法·青少年编程
谈笑也风生16 小时前
只出现一次的数字 II(一)
数据结构·算法·leetcode
蕓晨16 小时前
auto 自动类型推导以及注意事项
开发语言·c++·算法
mjhcsp16 小时前
C++ 递推与递归:两种算法思想的深度解析与实战
开发语言·c++·算法
_OP_CHEN16 小时前
算法基础篇:(三)基础算法之枚举:暴力美学的艺术,从穷举到高效优化
c++·算法·枚举·算法竞赛·acm竞赛·二进制枚举·普通枚举
m0_7482480216 小时前
《详解 C++ Date 类的设计与实现:从运算符重载到功能测试》
java·开发语言·c++·算法