当前位置:网站首页>Find the mode in the binary search tree (use medium order traversal as an ordered array)
Find the mode in the binary search tree (use medium order traversal as an ordered array)
2022-07-07 08:04:00 【Hydrion-Qlz】
501. The mode in the binary search tree
The difficulty is simple
Give you a binary search tree with duplicate values (BST) The root node root , Find out and return to BST All in The number of ( namely , The most frequent element ).
If there is more than one mode in the tree , Can press In any order return .
Assume BST Meet the following definitions :
- The value of the node contained in the left subtree of the node Less than or equal to The value of the current node
- The value of the node contained in the right subtree of the node Greater than or equal to The value of the current node
- Both the left and right subtrees are binary search trees
Ideas
The definition here is similar to what we usually encounter BST The definition of is different , Therefore, it should be clear that values equal to the current node may appear in both the left subtree and the right subtree , So it is very difficult to deal with recursion alone
But I am As mentioned in a previous question , Traverse the binary search tree , What you get is an ordered array .
Find the mode on an ordered array , Isn't it easy to catch ?
If you really don't understand it, you can go through the middle order first and add all the elements to an array , Then find the mode on the array
Of course, it is not recommended , This will lead to low time efficiency
package cn.edu.xjtu.carlWay.tree.findModeInBST;
import cn.edu.xjtu.Util.TreeNode.TreeNode;
import java.util.ArrayList;
import java.util.List;
/** * 501. The mode in the binary search tree * Give you a binary search tree with duplicate values (BST) The root node root , Find out and return to BST All in The number of ( namely , The most frequent element ). * <p> * If there is more than one mode in the tree , Can press In any order return . * <p> * Assume BST Meet the following definitions : * <p> * The value of the node contained in the left subtree of the node Less than or equal to The value of the current node * The value of the node contained in the right subtree of the node Greater than or equal to The value of the current node * Both the left and right subtrees are binary search trees * <p> * https://leetcode-cn.com/problems/find-mode-in-binary-search-tree/ */
public class Solution {
List<Integer> result = new ArrayList<>();
int maxNum = 0;
TreeNode pre;
int count = 0;
public int[] findMode(TreeNode root) {
if (root == null) return null;
traversal(root);
int[] ret = new int[result.size()];
for (int i = 0; i < ret.length; i++) {
ret[i] = result.get(i);
}
return ret;
}
private void traversal(TreeNode root) {
if (root == null) return;
// Recursive left subtree
traversal(root.left);
// Processing data
if (pre == null || root.val != pre.val) {
count = 1;
} else {
count++;
}
if (count > maxNum) {
result.clear();
result.add(root.val);
maxNum = count;
} else if (count == maxNum) {
result.add(root.val);
}
pre = root;
// Recursive right subtree
traversal(root.right);
}
}
边栏推荐
- 探索干货篇!Apifox 建设思路
- 央视太暖心了,手把手教你写HR最喜欢的简历
- 贝叶斯定律
- 3D reconstruction - stereo correction
- Force buckle 144 Preorder traversal of binary tree
- 2022 tea master (intermediate) examination questions and mock examination
- Info | webrtc M97 update
- Chip information website Yite Chuangxin
- JS quick start (I)
- Implementation of replacement function of shell script
猜你喜欢

探索干货篇!Apifox 建设思路

2022茶艺师(初级)考试题模拟考试题库及在线模拟考试

有 Docker 谁还在自己本地安装 Mysql ?

Problem solving: unable to connect to redis

Numbers that appear only once

Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!

Ansible
![[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)](/img/39/cac2b5492d374da393569e2ab467a4.png)
[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)

Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)

Few shot Learning & meta learning: small sample learning principle and Siamese network structure (I)
随机推荐
DNS server configuration
[mathematical notes] radian
Roulette chart 2 - writing of roulette chart code
Paddlepaddle 29 dynamically modify the network structure without model definition code (relu changes to prelu, conv2d changes to conv3d, 2D semantic segmentation model changes to 3D semantic segmentat
Pytest + allure + Jenkins Environment - - achèvement du remplissage de la fosse
2022年茶艺师(中级)考试试题及模拟考试
力扣(LeetCode)187. 重复的DNA序列(2022.07.06)
【数字IC验证快速入门】13、SystemVerilog interface 和 program 学习
芯片 設計資料下載
[UVM practice] Chapter 2: a simple UVM verification platform (2) only driver verification platform
Qt学习26 布局管理综合实例
[matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead
【数字IC验证快速入门】17、SystemVerilog学习之基本语法4(随机化Randomization)
Pytest+allure+jenkins installation problem: pytest: error: unrecognized arguments: --alluredir
开源生态|打造活力开源社区,共建开源新生态!
SQL优化的魅力!从 30248s 到 0.001s
Cnopendata list data of Chinese colleges and Universities
[unity] several ideas about circular motion of objects
[2022 ciscn] replay of preliminary web topics
Avatary的LiveDriver试用体验