首页 > 其他分享 >2.二叉树层序遍历

2.二叉树层序遍历

时间:2023-12-06 21:34:15浏览次数:32  
标签:node 遍历 return int 层序 que 二叉树 null root

107. 二叉树的层序遍历 II

相对于102.二叉树的层序遍历,就是最后把result数组反转一下就可以了。

class Solution {
    //利用链表可以进行o(1)头部插入
    public LinkedList<List<Integer>> res = new LinkedList<List<Integer>>();
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        /*
        order(root);
        List<List<Integer>> resBottom = new ArrayList<List<Integer>>();
        for(int i = res.size()-1;i>=0;i--){
            resBottom.add(res.get(i));
        }

        return resBottom;
        */

        order(root);
        return res;
    

    }
        
    
    //BFS-迭代法,使用队列
    public void order(TreeNode node){
        if(node == null){
            return;
        }
        Queue<TreeNode> que = new LinkedList<TreeNode>();
        que.offer(node);

        while(!que.isEmpty()){
            List<Integer> list = new ArrayList<Integer>();
            int deep = que.size();

            while(deep> 0){
                TreeNode cur = que.poll();
                list.add(cur.val);
                if(cur.left!=null){
                    que.offer(cur.left);
                }
                if(cur.right!=null){
                    que.offer(cur.right);
                }
                deep--;
            }
            //res.add(list);
            //插入到头部,满足层次返序
            res.addFirst(list);
        }
    }
}

199. 二叉树的右视图

层序遍历的时候,判断是否遍历到单层的最后面的元素,如果是,就放进result数组中,随后返回result就可以了。

class Solution {

    public List<Integer> res = new ArrayList<Integer>();
    public List<Integer> rightSideView(TreeNode root) {
        //队列 迭代
        //每次返回每层的最后一个字段即可
        order(root);
        return res;

    }

    public void order(TreeNode node){
        if(node == null){
            return;
        }
        Queue<TreeNode> que = new LinkedList<TreeNode>();
        que.offer(node);

        while(!que.isEmpty()){
            
            int deep = que.size();

            for(int i=1;i<=deep;i++){
                TreeNode cur = que.poll();
                
                if(cur.left!=null){
                    que.offer(cur.left);
                }
                if(cur.right!=null){
                    que.offer(cur.right);
                }
                //每层队列中最后的即最右边的
                if(i == deep){
                   res.add(cur.val); 
                }
            }
        
        }
    }
}

637. 二叉树的层平均值

本题就是层序遍历的时候把一层求个总和在取一个均值。

class Solution {
    public List<Double> res = new ArrayList<Double>();
    public List<Double> averageOfLevels(TreeNode root) {
        order(root);
        return res;
    }
    public void order(TreeNode node){
        if(node == null){
            return;
        }
        Queue<TreeNode> que = new LinkedList<TreeNode>();
        que.offer(node);

        while(!que.isEmpty()){
            
            int deep = que.size();
            double levelSum = 0.0;

            for(int i=1;i<=deep;i++){
                TreeNode cur = que.poll();
                levelSum +=cur.val;

                if(cur.left!=null){
                    que.offer(cur.left);
                }
                if(cur.right!=null){
                    que.offer(cur.right);
                }
                
            }
            res.add(levelSum / deep);
        }
    }
}

429. N 叉树的层序遍历

这道题依旧是模板题,只不过一个节点有多个孩子了

class Solution {
    public List<List<Integer>> res = new ArrayList<List<Integer>>();
    public List<List<Integer>> levelOrder(Node root) {
        order(root);
        return res;
    }
    public void order(Node node){
        if(node == null){
            return;
        }
        Queue<Node> que = new LinkedList<Node>();
        que.offer(node);

        while(!que.isEmpty()){
            List<Integer> list = new ArrayList<Integer>();
            int deep = que.size();

            for(int i =0;i<deep;i++){
                //N叉数的Node
                Node cur = que.poll();
                list.add(cur.val);
                //获取N叉数的孩子
                List<Node> children = cur.children;

                //判空都加进队列
                if(children == null || children.size() == 0){
                    continue;
                }

                for(Node child : children){
                    if(child != null){
                        que.offer(child);
                    }
                }
                
            }
            res.add(list);
        }
    }
}

515. 在每个树行中找最大值

层序遍历,取每一层的最大值

class Solution {
    public List<Integer> res = new ArrayList<>();
    public List<Integer> largestValues(TreeNode root) {
            order(root);
            return res;
    }

    public void order(TreeNode node){
        if(node == null){
            return;
        }
        Queue<TreeNode> que = new LinkedList<TreeNode>();
        que.offer(node);

        while(!que.isEmpty()){
           
            int deep = que.size();
            int max = Integer.MIN_VALUE;

            for(int i = 0;i< deep;i++){
                TreeNode cur = que.poll();
                max = Math.max(max,cur.val);

                
                if(cur.left!=null){
                    que.offer(cur.left);
                }
                if(cur.right!=null){
                    que.offer(cur.right);
                }
                
            }
            res.add(max);
        }
    }
}

116. 填充每个节点的下一个右侧节点指针

本题依然是层序遍历,只不过在单层遍历的时候记录一下本层的头部节点,然后在遍历的时候让前一个节点指向本节点就可以了

class Solution {
    public Node connect(Node root) {
        if(root==null) {return null;}
        if(root.left != null){
            root.left.next = root.right;
            if(root.next != null){
                root.right.next = root.next.left;
            }

            connect(root.left);
            connect(root.right);
        }
        return root;
    }
}

117. 填充每个节点的下一个右侧节点指针 II

这道题目说是二叉树,但116题目说是完整二叉树,其实没有任何差别,一样的代码一样的逻辑一样的味道

class Solution {
    public Node connect(Node root) {
        return order(root);
    }
    public Node order(Node node){
        if(node == null){
            return null;
        }
        Queue<Node> que = new LinkedList<Node>();
        que.offer(node); 

        while(!que.isEmpty()){
            int deep = que.size();

                Node cur = new Node();
                Node next = new Node();

                for(int i =0;i<deep;i++){
                    if(i==0){//取出一层头结点
                        cur = que.poll();
                        //把当前层头结点视为 next节点,以便操作左右节点入队列
                        next = cur;
                    }else{
                        next = que.poll();
                        //本层前一个节点next指向本节点
                        cur.next = next;
                        //当前视角cur 应为next节点,以便连接下个,没有了就连最后的NULL
                        cur = next;
                    }
                    if(next.left!=null){
                        que.offer(next.left);
                    }
                    if(next.right!=null){
                        que.offer(next.right);
                    }
                }
                //本层最后一个节点指NULL
                cur.next = null;
            }
                    return node;
  
        }
}

104. 二叉树的最大深度

使用迭代法的话,使用层序遍历是最为合适的,因为最大的深度就是二叉树的层数,和层序遍历的方式极其吻合。

在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度

class Solution {
    int depth = 0;
    int res=0;

    public int maxDepth(TreeNode root){
        //traverse(root);

        return order(root);
    }

    public int order(TreeNode node){
        if(node == null){
            return 0;
        }
        Queue<TreeNode> que = new LinkedList<TreeNode>();
        que.offer(node);
        //深度计数器
        int len = 0;

        while(!que.isEmpty()){
            
            int deep = que.size();

            while(deep> 0){
                TreeNode cur = que.poll();
                
                if(cur.left!=null){
                    que.offer(cur.left);
                }
                if(cur.right!=null){
                    que.offer(cur.right);
                }
                deep--;
            }
            //深度累加
            len++;
        }
        return len;
    }
    
    void traverse(TreeNode root){
        if(root == null){
            return;
        }

        depth++;
        res=Math.max(res,depth);
        traverse(root.left);
        traverse(root.right);
        depth--;
    }


}

111. 二叉树的最小深度

相对于 104.二叉树的最大深度 ,本题还也可以使用层序遍历的方式来解决,思路是一样的。

需要注意的是,只有当左右孩子都为空的时候,才说明遍历的最低点了。如果其中一个孩子为空则不是最低点

class Solution {
    public int minDepth(TreeNode root) {
        //return order(root);
        return reCurSion(root);
    }

    private int reCurSion(TreeNode node){
        if(node == null){return 0;}
        int leftDepth = reCurSion(node.left);
        int rightDepth = reCurSion(node.right);
        if(node.left==null){
            return rightDepth+1;
        }
        if(node.right == null){
            return leftDepth+1;
        }

        return Math.min(leftDepth,rightDepth)+1;
    }
    
    public int order(TreeNode node){
        if(node == null){
            return 0;
        }
        Queue<TreeNode> que = new LinkedList<TreeNode>();
        que.offer(node);
        //可操作
        int len=0;

        while(!que.isEmpty()){
            
            int deep = que.size();
            //最小深度赋值
            len++;

            for(int i=0; i<deep; i++){
                TreeNode cur = que.poll();

                //如果当前节点的左右孩子都为空,直接返回最小深度
                if(cur.left ==null && cur.right == null){
                    return len;
                }
                
                if(cur.left!=null){
                    que.offer(cur.left);
                }
                if(cur.right!=null){
                    que.offer(cur.right);
                }
           
            }
            //可操作
        }
        return len;
    }

}

标签:node,遍历,return,int,层序,que,二叉树,null,root
From: https://www.cnblogs.com/autumnmoonming/p/17880577.html

相关文章

  • 1.二叉树
    二叉树的种类满二叉树满二叉树:如果一棵二叉树只有度为0的结点和度为2的结点,并且度为0的结点在同一层上,则这棵二叉树为满二叉树。完全二叉树完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边......
  • 第5章. 二叉树
    二叉树一、树的基本概念节点、根节点、父节点、子节点、兄弟节点一棵树可以没有任何节点,称为空树一棵树可以只有一个节点,也就是只有根节点子树、左子树、右子树节点的度:子树的个数树的度:所有节点度中的最大值叶子节点:度为0的节点非叶子节点:度不为0的节点层数:根节点......
  • 17_二叉树的所有路径
    二叉树的所有路径给你一个二叉树的根节点root,按任意顺序,返回所有从根节点到叶子节点的路径。叶子节点是指没有子节点的节点。示例1:输入:root=[1,2,3,null,5]输出:["1->2->5","1->3"]示例2:输入:root=[1]输出:["1"]【思路】题目要求从根节点到叶子的路径,所以需要......
  • [LeetCode] 498. Diagonal Traverse 对角线遍历
    题目Givenanmxnmatrixmat,returnanarrayofalltheelementsofthearrayinadiagonalorder.思考最初在纸上写写画画试了很多想法,但都没能解决,真的。。太弱了TT。后来在YT上看了个印度老哥的题解才醍醐灌顶。在此尝试复述他的题解。这题就是说将一个二维矩阵......
  • 16_平衡二叉树
    平衡二叉树【题外话】二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数。(从上往下看)二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数。(从下往上看)小疑惑:为什么104.二叉树的最大深度中求的是二叉树的最大深度,也用的是后序遍历。(本质上求解的就是根节......
  • 15_完全二叉树的节点个数
    完全二叉树的节点个数给你一棵完全二叉树的根节点root,求出该树的节点个数。完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第h层,则该层包含1~2h个节点。示......
  • 二叉树创建及遍历
    #include<stdio.h>#include<iostream>usingnamespacestd;typedefcharTElemType;typedefvoidStatus;typedefintElemType;typedefstructBiTNode{ TElemTypedata; BiTNode*lchild,*rchild;}BiTNode,*BiTree;voidCreateBiTree(BiTree......
  • # yyds干货盘点 # 有一个数据对应表,遍历df数据只要df存在对应的数据就替换掉,但是这个
    大家好,我是皮皮。一、前言前几天在Python最强王者交流群【哎呦喂 是豆子~】问了一个Pandas数据处理的问题,一起来看看吧。问题描述:大佬们 请问下这个问题 有一个数据对应表,然后遍历df数据只要df存在对应的数据就替换掉但是这个一直报错(IndexError:index0isoutofboundsf......
  • 有一个数据对应表,遍历df数据只要df存在对应的数据就替换掉,但是这个一直报错
    大家好,我是皮皮。一、前言前几天在Python最强王者交流群【哎呦喂 是豆子~】问了一个Pandas数据处理的问题,一起来看看吧。问题描述:大佬们 请问下这个问题 有一个数据对应表,然后遍历df数据只要df存在对应的数据就替换掉但是这个一直报错(IndexError:index0isoutofboun......
  • 树的层序遍历算法框架
    1核心代码框架点击查看代码voidlevelOrder(TreeNode*root){if(!root)return;queue<TreeNode*>que;que.push(root);while(!que.empty()){intsize=que.size();for(inti=0;i<size;i++){TreeNode*cur=......