<p>Given the <code>root</code> of a binary search tree (BST) with duplicates, return <em>all the <a href="https://en.wikipedia.org/wiki/Mode_(statistics)" target="_blank">mode(s)</a> (i.e., the most frequently occurred element) in it</em>.</p>

<p>If the tree has more than one mode, return them in <strong>any order</strong>.</p>

<p>Assume a BST is defined as follows:</p>

<ul>
	<li>The left subtree of a node contains only nodes with keys <strong>less than or equal to</strong> the node&#39;s key.</li>
	<li>The right subtree of a node contains only nodes with keys <strong>greater than or equal to</strong> the node&#39;s key.</li>
	<li>Both the left and right subtrees must also be binary search trees.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/11/mode-tree.jpg" style="width: 142px; height: 222px;" />
<pre>
<strong>Input:</strong> root = [1,null,2,2]
<strong>Output:</strong> [2]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> root = [0]
<strong>Output:</strong> [0]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
	<li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
</ul>

<p>&nbsp;</p>
<strong>Follow up:</strong> Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).
/**
 * 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) {}
 * };
 */

struct table
{
    std::vector<std::pair<int, int>> table;
    void add(int x)
    {
        //if (table.size() <= table.capacity()/2)
        //    table.resize(table.size()+5);
        bool added = false;
        for (int i = 0; i < table.size(); ++i)
        {
            if (table[i].first == x)
            {
                ++table[i].second;
                added = true;
            }
        }
        if (!added)
            table.push_back({x,1});

        std::sort(table.begin(), table.end(), [](auto &left, auto &right) { return left.second > right.second; });
        //for (auto elem : table)
        //    std::cout << elem.first << ' ' << elem.second << '\n';
        //std::cout << "---------------------------------------";
    }
    std::vector<int> returnHighest()
    {
        //table.resize(table.size());
        //std
        std::vector<int> result = {};
        for (auto elem : table)
        {
            if (table[0].second == elem.second)
                result.push_back(elem.first);
        }
        return result;
    }
};

class Solution {
public:
    vector<int> findMode(TreeNode* root) {
        table table;
        traverse(root, table);
        return {table.returnHighest()};
    }
    void traverse(TreeNode* root, table& table)
    {
        if (root)
        {
            table.add(root->val);
            traverse(root->left, table);
            traverse(root->right, table);
        }
    }
};
24300