当前位置:网站首页>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);
}
}
边栏推荐
- 2022制冷与空调设备运行操作复训题库及答案
- Qt学习26 布局管理综合实例
- Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!
- Visualization Document Feb 12 16:42
- 微信小程序基本组件使用介绍
- Leetcode 43 String multiplication (2022.02.12)
- 【数字IC验证快速入门】15、SystemVerilog学习之基本语法2(操作符、类型转换、循环、Task/Function...内含实践练习)
- Recursive method constructs binary tree from middle order and post order traversal sequence
- 探索干货篇!Apifox 建设思路
- Es FAQ summary
猜你喜欢

QT learning 26 integrated example of layout management

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

青龙面板-今日头条

LeetCode简单题之判断一个数的数字计数是否等于数位的值

【数字IC验证快速入门】17、SystemVerilog学习之基本语法4(随机化Randomization)

【数字IC验证快速入门】12、SystemVerilog TestBench(SVTB)入门

2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions

Force buckle 144 Preorder traversal of binary tree

Sign up now | oar hacker marathon phase III, waiting for your challenge

2022制冷与空调设备运行操作复训题库及答案
随机推荐
青龙面板--花花阅读
Visualization Document Feb 12 16:42
Operation suggestions for today's spot Silver
Main window in QT learning 27 application
Avatary的LiveDriver试用体验
Use and analysis of dot function in numpy
快速使用 Jacoco 代码覆盖率统计
JS quick start (I)
A bit of knowledge - about Apple Certified MFI
Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)
Recursive method to verify whether a tree is a binary search tree (BST)
C language queue
Sign up now | oar hacker marathon phase III, waiting for your challenge
Zhilian + AV, AITO asked M7 to do more than ideal one
Li Kou interview question 04.01 Path between nodes
【数字IC验证快速入门】17、SystemVerilog学习之基本语法4(随机化Randomization)
Cnopendata list data of Chinese colleges and Universities
Quickly use Jacobo code coverage statistics
Codeforce c.strange test and acwing
Cnopendata geographical distribution data of religious places in China