当前位置:网站首页>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;
}
边栏推荐
- Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
- 【统计学习方法】学习笔记——第五章:决策树
- In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
- 【从 0 开始学微服务】【00】课程概述
- gcc 编译报错
- ICLR 2022 | pre training language model based on anti self attention mechanism
- [learn microservices from 0] [03] explore the microservice architecture
- 详解ThinkPHP支持的URL模式有四种普通模式、PATHINFO、REWRITE和兼容模式
- [difficult and miscellaneous]pip running suddenly appears modulenotfounderror: no module named 'pip‘
- mysql怎么创建,删除,查看索引?
猜你喜欢
Vxlan static centralized gateway
Several ways to clear floating
leetcode刷题:二叉树21(验证二叉搜索树)
AirServer自动接收多画面投屏或者跨设备投屏
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
leetcode刷题:二叉树26(二叉搜索树中的插入操作)
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
ACL 2022 | 序列标注的小样本NER:融合标签语义的双塔BERT模型
Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
随机推荐
Attack and defense world - PWN learning notes
SSM框架搭建的步骤
SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
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)
Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
Cookie
利用栈来实现二进制转化为十进制
【统计学习方法】学习笔记——第五章:决策树
mysql怎么创建,删除,查看索引?
ICLR 2022 | 基于对抗自注意力机制的预训练语言模型
[deep learning] image multi label classification task, Baidu paddleclas
leetcode刷题:二叉树23(二叉搜索树中的众数)
详解ThinkPHP支持的URL模式有四种普通模式、PATHINFO、REWRITE和兼容模式
JS to convert array to tree data
leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
leetcode刷题:二叉树24(二叉树的最近公共祖先)
[pytorch practice] image description -- let neural network read pictures and tell stories
图形对象的创建与赋值
2022 polymerization process test question simulation test question bank and online simulation test
xshell评估期已过怎么办