当前位置: 首页 > news >正文

【二叉搜索树】【前后指针】Leetcode 501. 二叉搜索树中的众数

【二叉搜索树】【前后指针】Leetcode 501. 二叉搜索树中的众数

    • 解法1 中序遍历+双指针
    • 解法2 我的复杂方法 先中序遍历到数组,之后hashmap遍历判断众数 之后转化为数组输出

在这里插入图片描述

---------------🎈🎈题目链接🎈🎈-------------------

解法1 中序遍历+双指针

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {List<Integer> mylist = new ArrayList<>();TreeNode pre = null;int count = 1;int maxcount = 1;public int[] findMode(TreeNode root) {// 中序递归+双指针pre cur比较helper(root);int[] result = new int[mylist.size()];for(int i = 0; i< mylist.size(); i++){result[i] = mylist.get(i);}return result;}public void helper(TreeNode root){// 中序遍历if(root == null) return;helper(root.left);//左//  // 计数if(pre==null || pre.val!=root.val){  // 初始化count 遇到不相等就置count为1 表示当前1个相同的元素count = 1;}else if(pre!=null && pre.val==root.val){ // 满足前后相等条件count++count ++;}//  // 更新结果集if(count > maxcount){maxcount = count;mylist.clear();mylist.add(root.val);}else if(count == maxcount){mylist.add(root.val);}pre = root;helper(root.right); // 右}
}        

解法2 我的复杂方法 先中序遍历到数组,之后hashmap遍历判断众数 之后转化为数组输出

一些方法汇总:

  1. 遍历hashmap的key-value
 for(var keyvalue:myhash.entrySet()){keyvalue.getKey();keyvalue.getValue();}

2.清空arraylist:arr.clear
3.最小值Integer.MIN_VALUE
4.ArrayList转化为int[ ]输出

int[] finalresult = new int[result.size()];for(int i = 0; i < result.size();i++){finalresult[i] = result.get(i);}

代码:::::

class Solution {public int[] findMode(TreeNode root) {List<Integer> mylist = new ArrayList<>();helper(root, mylist);// 取出众数HashMap<Integer,Integer> myhash = new HashMap<>();for(int i = 0; i < mylist.size();i++){myhash.put(mylist.get(i),myhash.getOrDefault(mylist.get(i),0)+1);}// 遍历hashint max = Integer.MIN_VALUE;List<Integer> result = new ArrayList<>();for(var keyvalue:myhash.entrySet()){if(keyvalue.getValue() > max){result.clear();result.add(keyvalue.getKey());max = keyvalue.getValue();}else if(keyvalue.getValue() == max){result.add(keyvalue.getKey());}}// 输出数组int[] finalresult = new int[result.size()];for(int i = 0; i < result.size();i++){finalresult[i] = result.get(i);}return finalresult;}public void helper(TreeNode root, List<Integer> mylist){  // 中序遍历的到数组里if(root == null) return ;helper(root.left,mylist);mylist.add(root.val);helper(root.right,mylist);}
}

相关文章:

  • UE4c++ ConvertActorsToStaticMesh ConvertProceduralMeshToStaticMesh
  • uniapp的h5端保存文件
  • Java中的Collection
  • 扩频通信信号的产生与接收
  • MySQL 常用优化方式
  • 学习:吴恩达:什么是神经元?神经网络如何工作?
  • idea生成WebServices接口
  • HTML超链接去下划线
  • vue3 + vite 项目可以使用纯Js开发吗?
  • Image Fusion via Vision-Language Model【文献阅读】
  • React Switch用法及手写Switch实现
  • 初学者如何学习python
  • leetcode 热题 100_三数之和
  • LeetCode——二叉树(Java)
  • 【Vue3】PostCss 适配
  • [译] 理解数组在 PHP 内部的实现(给PHP开发者的PHP源码-第四部分)
  • 〔开发系列〕一次关于小程序开发的深度总结
  • Angular 2 DI - IoC DI - 1
  • Brief introduction of how to 'Call, Apply and Bind'
  • Bytom交易说明(账户管理模式)
  • co.js - 让异步代码同步化
  • Hibernate最全面试题
  • HTTP--网络协议分层,http历史(二)
  • leetcode46 Permutation 排列组合
  • Map集合、散列表、红黑树介绍
  • maya建模与骨骼动画快速实现人工鱼
  • nfs客户端进程变D,延伸linux的lock
  • select2 取值 遍历 设置默认值
  • webpack4 一点通
  • yii2中session跨域名的问题
  • 给自己的博客网站加上酷炫的初音未来音乐游戏?
  • 基于 Ueditor 的现代化编辑器 Neditor 1.5.4 发布
  • 每天一个设计模式之命令模式
  • 前言-如何学习区块链
  • 如何在GitHub上创建个人博客
  • 首页查询功能的一次实现过程
  • 思考 CSS 架构
  • 延迟脚本的方式
  • ​ArcGIS Pro 如何批量删除字段
  • (31)对象的克隆
  • (C语言)二分查找 超详细
  • (c语言版)滑动窗口 给定一个字符串,只包含字母和数字,按要求找出字符串中的最长(连续)子串的长度
  • (python)数据结构---字典
  • (附源码)ssm智慧社区管理系统 毕业设计 101635
  • (接口自动化)Python3操作MySQL数据库
  • (新)网络工程师考点串讲与真题详解
  • (转)JAVA中的堆栈
  • (转)socket Aio demo
  • ***微信公众号支付+微信H5支付+微信扫码支付+小程序支付+APP微信支付解决方案总结...
  • .Net 6.0 处理跨域的方式
  • .net 7 上传文件踩坑
  • .net core 调用c dll_用C++生成一个简单的DLL文件VS2008
  • .NET Core6.0 MVC+layui+SqlSugar 简单增删改查
  • .NET Framework与.NET Framework SDK有什么不同?
  • .NET 事件模型教程(二)