代码随想录第十七天(107、199、637)

文章介绍了三种基于二叉树层次遍历的算法实现:1)层序遍历并反转结果,2)获取二叉树的右视图,3)计算每一层的平均值。每种方法都使用了队列进行层次遍历,关键在于遍历过程中的处理逻辑,如反转添加顺序、特定条件下的值记录等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

107. 二叉树的层序遍历 II

class Solution {
    
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> res=new ArrayList<List<Integer>>();
        Queue<TreeNode> que=new LinkedList<>();
        if(root==null){
            return res;
        }
        que.offer(root);
        while(!que.isEmpty()){
            int len=que.size();
            List<Integer> temp=new LinkedList<>();
            while(len>0){
                TreeNode cur=que.poll();
                temp.add(cur.val);
                if(cur.left!=null){
                    que.offer(cur.left);
                }
                if(cur.right!=null){
                    que.offer(cur.right);
                }
                len--;
            }
            res.add(temp);
        }
        List<List<Integer>> result=new ArrayList<List<Integer>>();
        for(int i=res.size()-1;i>=0;i--){
            result.add(res.get(i));
        }
        return result;
    }
}

就是在层序遍历的基础上,反转了一下

【补充】另外一种不用反转的写法
注意:addFirst方法是LinkedList中的,在声明返回数组时,声明的必须是:LinkedList< List< Integer>>,如果声明的是List< List< Integer>>会编译出错

class Solution {
    
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        LinkedList<List<Integer>> res=new LinkedList<List<Integer>>();
        Queue<TreeNode> que=new LinkedList<>();
        if(root==null){
            return res;
        }
        que.offer(root);
        while(!que.isEmpty()){
            int len=que.size();
            List<Integer> temp=new LinkedList<>();
            while(len>0){
                TreeNode cur=que.poll();
                temp.add(cur.val);
                if(cur.left!=null){
                    que.offer(cur.left);
                }
                if(cur.right!=null){
                    que.offer(cur.right);
                }
                len--;
            }
            // res.add(temp);
            res.addFirst(temp);
        }
        // List<List<Integer>> result=new ArrayList<List<Integer>>();
        // for(int i=res.size()-1;i>=0;i--){
        //     result.add(res.get(i));
        // }
        // return result;
        return res;
    }
}

199. 二叉树的右视图

思路:就是在层次遍历到这一层的最后一个节点时,将他加入到结果数组中。最后一个结点的判断就是,这一层所在的队列长度为1时。

class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> list=new LinkedList<>();
        Queue<TreeNode> que=new LinkedList<>();
        if(root==null){
            return list;
        }
        que.offer(root);
        while(!que.isEmpty()){
            int len=que.size();
            while(len>0){
                TreeNode cur=que.poll();
                if(cur.left!=null){
                    que.offer(cur.left);
                }
                if(cur.right!=null){
                    que.offer(cur.right);
                }
                if(len==1){
                    list.add(cur.val);
                }
                len--;
            }
        }
        return list;
    }
}

注意:必须要有if(root==null)这个判断条件,否则直接执行que.offer(root)这个条件出队列的cur也是null,会报空指针异常。

637

class Solution {
    public List<Double> averageOfLevels(TreeNode root) {
        List<Double> list=new LinkedList<>();
        Queue<TreeNode> que=new LinkedList<>();
        if(root==null){
            return list;
        }
        que.offer(root);
        while(!que.isEmpty()){
            int len=que.size();
            int length=len;
            double num=0;
            while(len>0){
                TreeNode cur=que.poll();
                num+=cur.val;
                if(cur.left!=null){
                    que.offer(cur.left);
                }
                if(cur.right!=null){
                    que.offer(cur.right);
                }
                len--;
            }
            list.add(num/length);
        }
        return list;
    }
}

如果不将num设置成double类型,最后的除法会是正数,将结果变为double类型才能进一步变成Double包装类,最简单的方法就是将num声明成double类型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值