# subject

Given a binary search tree (BST) with the same value, find all modes in BST.

(the most frequent element)

It is assumed that BST has the following definitions:

• The value of the node contained in the left subtree of the node is less than or equal to the value of the current node
• The value of the node contained in the right subtree of the node is greater than or equal to the value of the current node
• Both the left subtree and the right subtree are binary search trees

For example: given BST [1,null,2,2],

```   1
\
2
/
2
```

Tip: if the mode exceeds 1, the output order does not need to be considered#

# Problem solution

## General traversal

Ignoring the nature of binary search tree, the binary tree is traversed directly.

At the same time, the hash table is used to count the frequency of different values.

Sort the hash table, and then traverse the map to find one or more groups of values with the highest frequency.

```/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
// Write two private functions to traverse the BST
private:
void searchBST(TreeNode* cur, unordered_map<int, int> &map) {
// Set termination conditions
if (cur == nullptr) return;
map[cur->val]++;
searchBST(cur->left, map);
searchBST(cur->right, map);
}
// Custom sort comparison function
// static modifies that the file can only be invoked in this file.
bool static cmp(pair<int, int> a, pair<int, int> b) {
return a.second > b.second;
}
public:
vector<int> findMode(TreeNode* root) {
vector<int> res;
unordered_map<int, int> map;
// Consider special circumstances first
if (root == nullptr) return res;
searchBST(root, map);
vector<pair<int, int>> vec(map.begin(), map.end());
// First use the custom comparison rule to sort
sort(vec.begin(), vec.end(), cmp);
res.push_back(vec.first);
// Also consider parallel elements with the same frequency
for (int i = 1; i < vec.size(); i++) {
if (vec[i].second == vec.second)
res.push_back(vec[i].first);
else break;
}
return res;
}
};
```

## BST properties

The main properties of Binary Search Tree are:

• The value of the node contained in the left subtree of the node is less than or equal to the value of the current node
• The value of the node contained in the right subtree of the node is greater than or equal to the value of the current node

Therefore, if the binary tree is traversed in middle order, the number list must be monotonically increasing, and the equal values must be adjacent.

```// Set the recursive termination condition first
if (cur == nullptr) return;
// Medium order traversal
searchBST(cur->left);
// Process current node
if (pre == nullptr) {
// Description is currently the root node
count = 1;
} else if (pre->val == cur->val) {
// The values of adjacent nodes are the same, and the atoms increase
count++;
} else {
// Count again if the critical node value is different
count = 1;
}
pre = cur;
```

Therefore, when traversing the binary tree, we set a pre to locate the previous node of the current node. Because the nodes of adjacent values must be adjacent, there are three cases:

• Root node: when traversing the first node, pre is empty. At this time, the initialization frequency is:

count = 1;

• Pre - > Val = = cur - > Val: at this time, the count of the current value is incremented:

count++;

• pre->val != Cur - > Val: the current value has been updated and the count needs to be reset

count = 1;

Each time we update the count, we need to compare it with maxCount:

```if (count == maxCount) {
res.push_back(cur->val);
}

if (count > maxCount) {
// Refresh maxCount
maxCount = count;
// All previous results will be deleted
// Including the record breaking value itself will be cleared before inserting (I kill myself)
res.clear();
res.push_back(cur->val);
}
```
• If the value is equal to maxCount, it may be the value with the first frequency, and it will be temporarily added to the result set.

• If the value is not equal to maxCount, it means that the previous maxCount is not the real highest frequency, and the existing result set needs to be cleared. Then fill the value corresponding to the current highest frequency word into the result set.

Because the current result set has also gone through the stage of count == maxCount, the current val will be emptied and refilled together. I delete myself:).

The complete code is as follows:

```/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
private:
int count;
int maxCount;
vector<int> res;
TreeNode* pre;
// pre is used to record the previous node for numerical comparison and counting
void searchBST(TreeNode* cur) {
// Set the recursive termination condition first
if (cur == nullptr) return;
// Medium order traversal
searchBST(cur->left);
// Process current node
if (pre == nullptr) {
// Description is currently the root node
count = 1;
} else if (pre->val == cur->val) {
// The values of adjacent nodes are the same, and the atoms increase
count++;
} else {
// Count again if the critical node value is different
count = 1;
}
pre = cur;
// Compare with maxcount after counting
if (count == maxCount) {
res.push_back(cur->val);
}

if (count > maxCount) {
// Refresh maxCount
maxCount = count;
// All previous results will be deleted
// Including the record breaking value itself will be cleared before inserting (I kill myself)
res.clear();
res.push_back(cur->val);
}
searchBST(cur->right);

}
public:
vector<int> findMode(TreeNode* root) {
if (root == nullptr) return res;
searchBST(root);
return res;
}
};
```

Posted on Tue, 30 Nov 2021 00:39:17 -0500 by rulkster2