当前位置:网站首页>Leetcode skimming: binary tree 23 (mode in binary search tree)
Leetcode skimming: binary tree 23 (mode in binary search tree)
2022-07-07 12:48:00 【Taotao can't learn English】
501. The mode in the binary search tree
Given a binary search tree with the same value (BST), find BST All the modes in ( The most frequent element ).
Assume BST It has the following definition :
- The value of the node contained in the left subtree of a node is less than or equal to the value of the current node
- The value of the node contained in the right subtree of a node is greater than or equal to the value of the current node
- Both the left and right subtrees are binary search trees
for example :
Given BST [1,null,2,2],

return [2].
Tips : If the mode exceeds 1 individual , There is no need to consider the output sequence
Advanced : Can you not use extra space ?( Suppose the overhead of implicit call stack generated by recursion is not counted )
Two ways of thinking :
1: General writing method , Hashtable , Traversing the tree , Save the result set to hashmap, Traversal frequency value Get the maximum , Iterate again to get the result set , Process into an array .
HashMap<Integer, Integer> result = new HashMap<Integer, Integer>();
public int[] findMode(TreeNode root) {
// This problem can be done with a hash table
traversal(root);
Set<Integer> keys = result.keySet();
int max = -1;
for (Integer key : keys) {
if (result.get(key) > max) {
max = result.get(key);
}
}
Set<Integer> resultSet = new HashSet<Integer>();
for (Integer key : keys) {
if (result.get(key) == max) {
resultSet.add(key);
}
}
int[] resultArr = new int[resultSet.size()];
int index = 0;
for (Integer key : resultSet) {
resultArr[index] = key;
index++;
}
return resultArr;
}
public void traversal(TreeNode root) {
if (root == null) {
return;
}
traversal(root.left);
if (result.containsKey(root.val)) {
result.put(root.val, result.get(root.val) + 1);
} else {
result.put(root.val, 1);
}
traversal(root.right);
}

2. Binary search tree writing : Because it's a binary search tree , The decreasing , So the same number must be together , Count the number of occurrences of each number , Give a maximum number of occurrences , If the number of occurrences of the current number is less than the maximum number , Do not change , If equal , Then add the current number to the result set , If it is greater than the maximum number of occurrences , Then empty the result set , The current number is added to the result set .
List<Integer> list = new ArrayList<>();
TreeNode parent;
int maxTimes = 0;
int curTimes = 0;
public void inorderTraversal(TreeNode root) {
if (root == null) {
return;
}
inorderTraversal(root.left);
if (root.val == parent.val) {
curTimes++;
// If the number of occurrences of the current element is greater than the maximum number , Then a new mode appears , The original is not the mode
if (curTimes > maxTimes) {
maxTimes = curTimes;
list.clear();
list.add(root.val);
} else if (curTimes == maxTimes) {
// If the number of occurrences of the current element is equal to the maximum number , Then a new mode appears
list.add(root.val);
}
} else {
curTimes = 1;
if (curTimes > maxTimes) {
maxTimes = curTimes;
list.clear();
list.add(root.val);
} else if (curTimes == maxTimes) {
// If the number of occurrences of the current element is equal to the maximum number , Then a new mode appears
list.add(root.val);
}
parent = root;
}
inorderTraversal(root.right);
}
public int[] findMode(TreeNode root) {
parent = root;
inorderTraversal(root);
int index = 0;
int[] resultArr = new int[list.size()];
for (Integer result : list) {
resultArr[index] = result;
index++;
}
return resultArr;
}

边栏推荐
- leetcode刷题:二叉树19(合并二叉树)
- 对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
- Realize all, race, allsettled and any of the simple version of promise by yourself
- [learn microservice from 0] [01] what is microservice
- 2022 examination questions and online simulation examination for safety production management personnel of hazardous chemical production units
- Common knowledge of one-dimensional array and two-dimensional array
- 数据库安全的重要性
- 有什么类方法或是函数可以查看某个项目的Laravel版本的?
- [statistical learning method] learning notes - support vector machine (Part 2)
- SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
猜你喜欢

Financial data acquisition (III) when a crawler encounters a web page that needs to scroll with the mouse wheel to refresh the data (nanny level tutorial)

Image pixel read / write operation

Day-15 common APIs and exception mechanisms

2022聚合工艺考试题模拟考试题库及在线模拟考试

2022A特种设备相关管理(锅炉压力容器压力管道)模拟考试题库模拟考试平台操作

SQL head injection -- injection principle and essence
![[pytorch practice] write poetry with RNN](/img/91/a6d3f348ff099b7c44eb185921b1b6.png)
[pytorch practice] write poetry with RNN

Airserver automatically receives multi screen projection or cross device projection

ICLR 2022 | 基于对抗自注意力机制的预训练语言模型

RHSA first day operation
随机推荐
详解ThinkPHP支持的URL模式有四种普通模式、PATHINFO、REWRITE和兼容模式
[difficult and miscellaneous]pip running suddenly appears modulenotfounderror: no module named 'pip‘
[learn wechat from 0] [00] Course Overview
[crawler] avoid script detection when using selenium
【从 0 开始学微服务】【01】什么是微服务
密码学系列之:在线证书状态协议OCSP详解
【从 0 开始学微服务】【00】课程概述
[learn microservice from 0] [01] what is microservice
ACL 2022 | 序列标注的小样本NER:融合标签语义的双塔BERT模型
Utiliser la pile pour convertir le binaire en décimal
[statistical learning methods] learning notes - improvement methods
Realize all, race, allsettled and any of the simple version of promise by yourself
xshell评估期已过怎么办
Talk about four cluster schemes of redis cache, and their advantages and disadvantages
【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移
[爬虫]使用selenium时,躲避脚本检测
Charles: four ways to modify the input parameters or return results of the interface
[pytorch practice] image description -- let neural network read pictures and tell stories
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
Four functions of opencv