当前位置:网站首页>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);
}
}
边栏推荐
- C language flight booking system
- Common validation comments
- 2022 tea master (intermediate) examination questions and mock examination
- Codeforce c.strange test and acwing
- Qt学习26 布局管理综合实例
- OpenJudge NOI 2.1 1752:鸡兔同笼
- Force buckle 144 Preorder traversal of binary tree
- Recursive construction of maximum binary tree
- Binary tree and heap building in C language
- [unity] several ideas about circular motion of objects
猜你喜欢

2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案

LeetCode 90:子集 II

Leetcode 90: subset II

追风赶月莫停留,平芜尽处是春山

Hands on deep learning (IV) -- convolutional neural network CNN

Record a stroke skin bone error of the skirt

QT learning 28 toolbar in the main window

2022焊工(初级)判断题及在线模拟考试

SQL优化的魅力!从 30248s 到 0.001s

LeetCode简单题之找到一个数字的 K 美丽值
随机推荐
Quickly use Jacobo code coverage statistics
Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!
2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
dash plotly
C language queue
Hands on deep learning (IV) -- convolutional neural network CNN
Detailed explanation of Kalman filter for motion state estimation
力扣(LeetCode)187. 重复的DNA序列(2022.07.06)
Es FAQ summary
探索干货篇!Apifox 建设思路
Recursive construction of maximum binary tree
2022年茶艺师(中级)考试试题及模拟考试
Linux server development, redis protocol and asynchronous mode
Value sequence (subsequence contribution problem)
芯片资料 网站 易特创芯
Linux server development, MySQL transaction principle analysis
【数字IC验证快速入门】12、SystemVerilog TestBench(SVTB)入门
Leetcode 43 String multiplication (2022.02.12)
Rust versus go (which is my preferred language?)
Linux server development, MySQL index principle and optimization