The trick here is to name the state correctly, then let the implementation follow. For 501. Find Mode in Binary Search Tree, the solution in this repository is mainly a two pointers solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: two pointers, sliding window.
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are findMode, traverse.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- The queue gives the solution a level-by-level or frontier-style traversal.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01/**Using map**/
02
03//Runtime: 36 ms, faster than 51.88% of C++ online submissions for Find Mode in Binary Search Tree.
04//Memory Usage: 24.9 MB, less than 49.51% of C++ online submissions for Find Mode in Binary Search Tree.
05
06/**
07 * Definition for a binary tree node.
08 * struct TreeNode {
09 * int val;
10 * TreeNode *left;
11 * TreeNode *right;
12 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
13 * };
14 */
15class Solution {
16public:
17 vector<int> findMode(TreeNode* root) {
18 if(root == NULL) return vector<int>();
19 map<int, int> count;
20 queue<TreeNode*> q;
21 TreeNode* cur;
22 int maxcount = 0;
23 vector<int> ans;
24
25 q.push(root);
26
27 while(!q.empty()){
28 cur = q.front();
29 q.pop();
30
31 if(count.find(cur->val) == count.end()){
32 count[cur->val] = 1;
33 }else{
34 count[cur->val]++;
35 }
36 maxcount = max(maxcount, count[cur->val]);
37
38 if(cur->left) q.push(cur->left);
39 if(cur->right) q.push(cur->right);
40
41 }
42
43 for(map<int,int>::iterator it = count.begin(); it!=count.end(); it++){
44 if(it->second == maxcount) ans.push_back(it->first);
45 }
46
47 return ans;
48 }
49};
50
51/**Without map**/
52//https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/98100/Java-4ms-Beats-100-Extra-O(1)-solution-No-Map
53//Runtime: 28 ms, faster than 96.51% of C++ online submissions for Find Mode in Binary Search Tree.
54//Memory Usage: 23.2 MB, less than 97.09% of C++ online submissions for Find Mode in Binary Search Tree.
55
56class Solution {
57public:
58 vector<int> ans;
59 int prev = INT_MIN;
60 int curcount = 1; //since we only update curcount when it's not first node
61 int maxcount = 0;
62
63 void traverse(TreeNode* root){
64 if(!root) return;
65 traverse(root->left);
66 //we only update curcount when it's not first node
67 if(prev != INT_MIN){
68 if(root->val == prev){
69 curcount++;
70 }else{
71 curcount = 1;
72 }
73 }
74 prev = root->val;
75 if(curcount > maxcount){
76 maxcount = curcount;
77 ans.clear();
78 // cout << "if: " << curcount << " " << maxcount << " " << root->val << endl;
79 ans.push_back(root->val);
80 }else if(curcount == maxcount){
81 // cout << "else: " << curcount << " " << maxcount << " " << root->val << endl;
82 ans.push_back(root->val);
83 }
84 traverse(root->right);
85 }
86
87 vector<int> findMode(TreeNode* root) {
88 if(!root) return vector<int>();
89 traverse(root);
90 cout << endl;
91 return ans;
92 }
93};
Cost