码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【LeetCode-简单题】501. 二叉搜索树中的众数


    文章目录

      • 题目
      • 方法一:暴力哈希
      • 方法二:利用二叉搜索树的特性(递归+双指针)

    题目

    在这里插入图片描述

    方法一:暴力哈希

    这是针对于普通二叉树的解法 统计number出现次数 然后将次数最大的众数集 取出来

     Map<Integer , Integer > map = new HashMap<>();
         PriorityQueue<int[]> priori = new PriorityQueue<>((a,b)->b[1]-a[1]);//优先队列  按数组第二个元素从大到小排
        List<Integer> list = new ArrayList<>();
        public int[] findMode(TreeNode root) {
           dfs(root);
           for(Map.Entry<Integer, Integer>  num : map.entrySet()) priori.offer(new int[]{num.getKey(),num.getValue()});
           int max = priori.peek()[1];
           int size =priori.size();
           for(int i = 0 ; i<size ; i++){
               int [] a1 = priori.poll();
               if(max == a1[1]) list.add(a1[0]);
           } 
        	return list.stream().mapToInt(Integer::intValue).toArray(); 
        }
        public void dfs(TreeNode root){
            if(root == null) return;
            map.put(root.val,map.getOrDefault(root.val, 0)+1);
            dfs(root.left);
            dfs(root.right);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    方法二:利用二叉搜索树的特性(递归+双指针)

    关键在于本层逻辑的处理

    维护一个最大频率maxcount、单数字统计频率count、当前节点root的前一个节点 pre、

    在这里插入图片描述

    class Solution {
    
        List<Integer> list = new ArrayList<>();
        TreeNode pre = null;// 记录前一个节点
        int maxcount = 0; // 最大频率
        int count = 0;// 统计频率
        public int[] findMode(TreeNode root) {
           dfs(root);
          
           return list.stream().mapToInt(Integer::intValue).toArray();
        
        }
        public void dfs(TreeNode root){
            if(root == null) return;
    
            dfs(root.left);
            
            if(pre == null) count = 1;// 处理第一个节点
            else if(root.val == pre.val) count++;// 与前一个节点数值相同
            else count = 1;// 与前一个节点数值不同
    
            pre = root;
    
            if(count == maxcount) list.add(root.val);// 如果和最大值相同,放进result中
            else if(count>maxcount){// 如果计数大于最大值频率
                maxcount = count;// 更新最大频率
                list.clear(); // 很关键的一步,不要忘记清空result,之前result里的元素都失效了
                list.add(root.val);//再把此时的root放进result中
            }
    
            dfs(root.right);
            
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
  • 相关阅读:
    基于云服务MRS构建DolphinScheduler2调度系统
    绝活!十年高工带你详解Spring Cloud 架构
    springboot 从环境变量读取配置的流程
    机器学习06|两万字:决策树 【jupyter代码详解篇】
    Doris数据库BE——冷热数据方案
    【sciter】 组件化编程解决事件冒泡不生效
    老电脑装什么系统流畅
    Java项目实战《苍穹外卖》 一、项目概述
    用MQTT.fx模拟温度设备联调阿里云IOT物联网平台
    杰理之SPP 连接和断开事件处理【篇】
  • 原文地址:https://blog.csdn.net/weixin_45618869/article/details/133273752
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号