当前位置:网站首页>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);
}
}
边栏推荐
- Force buckle 144 Preorder traversal of binary tree
- Li Kou interview question 04.01 Path between nodes
- Pytest + allure + Jenkins Environment - - achèvement du remplissage de la fosse
- 贝叶斯定律
- 追风赶月莫停留,平芜尽处是春山
- Thinkcmf6.0 installation tutorial
- 芯片 設計資料下載
- 这5个摸鱼神器太火了!程序员:知道了快删!
- Content of string
- [Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替
猜你喜欢
Explore dry goods! Apifox construction ideas
Quickly use Jacobo code coverage statistics
Qt学习28 主窗口中的工具栏
Bugku CTF daily one question chessboard with only black chess
Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!
LeetCode简单题之判断一个数的数字计数是否等于数位的值
Explore Cassandra's decentralized distributed architecture
[quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
Force buckle 145 Binary Tree Postorder Traversal
【數字IC驗證快速入門】15、SystemVerilog學習之基本語法2(操作符、類型轉換、循環、Task/Function...內含實踐練習)
随机推荐
这5个摸鱼神器太火了!程序员:知道了快删!
Who has docker to install MySQL locally?
A bit of knowledge - about Apple Certified MFI
LeetCode简单题之字符串中最大的 3 位相同数字
Most elements
Linux server development, MySQL process control statement
Value sequence (subsequence contribution problem)
Summary of redis functions
Button wizard script learning - about tmall grabbing red envelopes
Cnopendata list data of Chinese colleges and Universities
芯片 設計資料下載
Pytest+allure+jenkins environment -- completion of pit filling
Common validation comments
paddlepaddle 29 无模型定义代码下动态修改网络结构(relu变prelu,conv2d变conv3d,2d语义分割模型改为3d语义分割模型)
LeetCode 40:组合总和 II
2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
Wechat applet data binding multiple data
Operation suggestions for today's spot Silver
Numbers that appear only once
Recursive method constructs binary tree from middle order and post order traversal sequence