当前位置:网站首页>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;
}

边栏推荐
- 【从 0 开始学微服务】【00】课程概述
- Preorder, inorder and postorder traversal of binary tree
- leetcode刷题:二叉树25(二叉搜索树的最近公共祖先)
- ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics
- OSPF exercise Report
- Simple implementation of call, bind and apply
- Image pixel read / write operation
- 什么是ESP/MSR 分区,如何建立ESP/MSR 分区
- [statistical learning method] learning notes - support vector machine (I)
- SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
猜你喜欢

Connect to blog method, overload, recursion

如何将 @Transactional 事务注解运用到炉火纯青?

2022a special equipment related management (boiler, pressure vessel and pressure pipeline) simulated examination question bank simulated examination platform operation

聊聊Redis缓存4种集群方案、及优缺点对比

Day-16 set

【统计学习方法】学习笔记——支持向量机(上)

【PyTorch实战】图像描述——让神经网络看图讲故事

2022 practice questions and mock examination of the third batch of Guangdong Provincial Safety Officer a certificate (main person in charge)

Multi row and multi column flex layout

2022聚合工艺考试题模拟考试题库及在线模拟考试
随机推荐
BGP third experiment report
Polymorphism, final, etc
2022A特种设备相关管理(锅炉压力容器压力管道)模拟考试题库模拟考试平台操作
What if does not match your user account appears when submitting the code?
2022-07-07日报:GAN发明者Ian Goodfellow正式加入DeepMind
2022危险化学品生产单位安全生产管理人员考题及在线模拟考试
对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
[statistical learning methods] learning notes - improvement methods
[learn microservice from 0] [01] what is microservice
Several ways to clear floating
Master公式。(用于计算递归的时间复杂度。)
[Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
【PyTorch实战】图像描述——让神经网络看图讲故事
[learn micro services from 0] [02] move from single application to service
mysql怎么创建,删除,查看索引?
Simple implementation of call, bind and apply
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
Customize the web service configuration file