给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素)。

假定 BST 有如下定义:

  • 结点左子树中所含结点的值小于等于当前结点的值
  • 结点右子树中所含结点的值大于等于当前结点的值
  • 左子树和右子树都是二叉搜索树

例如:

给定 BST [1,null,2,2],

1 \ 2 / 2

返回[2].

提示:如果众数超过1个,不需考虑输出顺序

class Solution {
public:
map<int, int> check;
int MAX = 0;
vector<int> findMode(TreeNode* root)
{
Fun(root);
map<int, int>:: iterator itr;
vector<int> res;
for(itr = check.begin(); itr != check.end(); itr++)
{
if(itr ->second == MAX)
{
res.push_back(itr ->first);
}
}
return res;
} void Fun(TreeNode* root)
{
if(root == NULL)
return ;
check[root ->val]++;
MAX = max(MAX, check[root ->val]);
Fun(root ->left);
Fun(root ->right);
}
};

Leetcode501.Find Mode in Binary Search Tree二叉搜索树中的众数的更多相关文章

  1. [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

  2. [LeetCode] Search in a Binary Search Tree 二叉搜索树中搜索

    Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...

  3. Leetcode700.Search in a Binary Search Tree二叉搜索树中的搜索

    给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. class Solution { public: ...

  4. LeetCode Recover Binary Search Tree——二查搜索树中两个节点错误

    Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing ...

  5. Java实现 LeetCode 501 二叉搜索树中的众数

    501. 二叉搜索树中的众数 给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素). 假定 BST 有如下定义: 结点左子树中所含结点的值小于等于当前结点的值 结点 ...

  6. [Swift]LeetCode501. 二叉搜索树中的众数 | Find Mode in Binary Search Tree

    Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...

  7. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  8. PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...

  9. [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

随机推荐

  1. 前端面试题之一JAVASCRIPT(算法类)

    一.JS操作获取和设置cookie //创建cookie function setcookie(name, value, expires, path, domain, secure) { var co ...

  2. laravel框架5.5 连接redis和redis集群

    'redis' => [ 'client' => 'predis', 'default' => [ 'host' => env('REDIS_HOST', '127.0.0.1 ...

  3. 洛谷P1291 [SHOI2002]百事世界杯之旅

    题目链接: kma 题目分析: 收集邮票的弱弱弱弱化版,因为是期望,考虑倒推 设\(f[i]\)表示现在已经买齐了\(i\)种,距离买完它的剩余期望次数 那么下一次抽有\(\frac{i}{n}\)的 ...

  4. kafka分析

    目录 1,kafka简介 2, Kafka Server 2.1,kafka中zookeeper的作用 2.2, Broker 2.2.1,Broker高性能设计 2.2.2,Broker选举机制 2 ...

  5. Cocos2d-x发布Android.mk 导入所有cpp

    #traverse all the directory and subdirectorydefine walk $(wildcard $(1)) $(foreach e, $(wildcard $(1 ...

  6. org.apache.commons工具类方法解释 转

    在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...

  7. Apache ActiveMQ教程

    一.特性及优势 1.实现JMS1.1规范,支持J2EE1.4以上 2.可运行于任何jvm和大部分web容器(ActiveMQ works great in any JVM) 3.支持多种语言客户端(j ...

  8. 全面解决Html页面缓存的问题

    页面缓存的问题可能大家都遇到过,很多功能做完没起效果,那么怎么解决这个问题呢?这里给出我的使用的解决方法 对于一个html页面,缓存分3部分,一个是页面内容,一个是css样式,一个是JS文件1.页面内 ...

  9. Django项目:CRM(客户关系管理系统)--60--50PerfectCRM实现CRM客户报名流程学生合同URL随机码

    # sales_urls.py # ————————47PerfectCRM实现CRM客户报名流程———————— from django.conf.urls import url from bpm. ...

  10. leetcode 696

    696. Count Binary Substrings Input: "00110011" Output: 6 Explanation: There are 6 substrin ...