剑指offer44:二叉树中每层的最大值

题目:
给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值。
例子:输入: root = [1,3,2,5,3,null,9]
输出: [1,3,9]
解释:
1
/
3 2
/ \ \
5 3 9
分析:
这道题两种思路:
1.第一个思路是利用一个队列和两个变量来实现,current变量用来记录队列中当前层个数,next变量用来记录队列中下一层元素的个数,根节点插入队列,将current初始化为1,之后逐个从队列中取出节点遍历,每当从队列中取出一个节点时,当前层剩余节点就少1个current数量就减1,,如果当前遍历的节点有子节点,那么将子节点插入队列中,由于子节点都位于当前遍历节点的下一层,因此队列中添加一个子节点,变量next数量加1。当current的数值变为0时,当前层的所有节点都已经遍历完了通过MATH的max函数,通过之前的出队比较就能得出当前层的最大值,之后将current的值设置为next的值,并把next初始化为0,重复这个过程,直到遍历完所有节点。
2.第二个思路时利用两个队列实现,第一个队列存当前层节点,第二个队列存下一层节点,开始将根节点入队队列1,之后每从队列中取出一个节点遍历,如果当前层存在子节点,将子节点入队队列2,这样就将每一层分开了,当第一个队列为空时,也就是当前队列都被遍历完了,能找到这一层的最大值了,在开始遍历下一层前,把队列queue1指向queue2,并将queue2重新初始化为空,重复此过程,直到所有节点遍历完为止。

代码:

import java.util.LinkedList;
import java.util.List;

public class LargestValues {
//    利用一个队列实现
    public List<Integer> largestValues1(TreeNode root){
        int current = 0;
        int next=0;
        LinkedList<TreeNode> queue = new LinkedList<>();
        int max = Integer.MIN_VALUE;
        if (root !=null){
            queue.offer(root);
            current=1;
        }
        LinkedList<Integer> result = new LinkedList<>();
        while (!queue.isEmpty()){
            TreeNode node = queue.poll();
            current--;
            max = Math.max(max,node.val);
            if (node.left !=null){
                queue.offer(node.left);
                next++;
            }
            if (node.right !=null){
                queue.offer(node.right);
                next++;
            }
            if (current == 0){
                result.add(max);
                max = Integer.MIN_VALUE;
                current = next;
                next = 0;
            }
        }
        return result;
    }
//    利用两个队列实现
    public List<Integer> largestValues2(TreeNode root){
        LinkedList<TreeNode> queue1 = new LinkedList<>();
        LinkedList<TreeNode> queue2 = new LinkedList<>();
        if (root!=null){
            queue1.offer(root);
        }
        LinkedList<Integer> result = new LinkedList<>();
        int max = Integer.MIN_VALUE;
        while (!queue1.isEmpty()){
            TreeNode node = queue1.poll();
            max = Math.max(max,node.val);
            if (node.left !=null){
                queue2.offer(node.left);
            }
            if (node.right !=null){
                queue2.offer(node.right);
            }
            if (queue1.isEmpty()){
                result.add(max);
                max = Integer.MIN_VALUE;
                queue1 = queue2;
                queue2 = new LinkedList<>();
            }
        }
        return result;
    }
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龙崎流河

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值