Skip to content

Commit f10a8aa

Browse files
Added solutions
1 parent 89d5c3c commit f10a8aa

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

find_mode_binary_search_tree.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
int getModeCount(TreeNode* root, unordered_map<int, int> &umap){
13+
if(root == NULL) return 0;
14+
umap[root->val]++;
15+
return max(umap[root->val], max(getModeCount(root->left, umap), getModeCount(root->right, umap)));
16+
17+
//max of max..
18+
//get mode count from map..
19+
//find the pair in map with mc = modecount and then append all the node->data with same mc to the result vector
20+
}
21+
22+
vector<int> findMode(TreeNode* root) {
23+
unordered_map<int, int> umap; //unordered because we dont need order :p
24+
vector<int> result;
25+
26+
int modeCount = getModeCount(root, umap);
27+
for(auto it : umap){
28+
if(it.second == modeCount)
29+
result.push_back(it.first);
30+
}
31+
return result;
32+
}
33+
};

reverse_level_order_traversal.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
vector<vector<int>> result;
13+
14+
void BFS(TreeNode *root, int level){
15+
if(root == NULL) return;
16+
17+
if(level == result.size()) result.push_back(vector<int>());
18+
19+
result[level].push_back(root->val);
20+
21+
BFS(root->left, level + 1);
22+
BFS(root->right, level + 1);
23+
24+
}
25+
vector<vector<int>> levelOrderBottom(TreeNode* root) {
26+
27+
if(root == NULL) return vector<vector<int>>();
28+
BFS(root, 0);
29+
return vector<vector<int>> (result.rbegin(), result.rend());
30+
}
31+
};

0 commit comments

Comments
 (0)