Skip to content

Commit 272660f

Browse files
committed
More code solution
1 parent 133a3c4 commit 272660f

24 files changed

+907
-0
lines changed

Balanced_Binary_Tree.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for binary tree
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+
bool isBalanced(TreeNode *root) {
13+
if(root == nullptr) return true;
14+
15+
int h = is_balanced(root);
16+
17+
if(h < 0) return false;
18+
return true;
19+
}
20+
private:
21+
int is_balanced(TreeNode *root){
22+
if(root == nullptr) return 0;
23+
if(root->left == nullptr && root->right == nullptr) return 1;
24+
25+
int left = is_balanced(root->left);
26+
if(left < 0) return -1;
27+
28+
int right = is_balanced(root->right);
29+
if(right < 0) return -1;
30+
31+
if(abs(left-right) > 1) return -1;
32+
else return max(left, right)+1;
33+
}
34+
35+
};

Binary_Tree_Inorder_Traversal.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for binary tree
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<int> inorderTraversal(TreeNode *root) {
13+
if(root == nullptr) return vector<int>();
14+
15+
vector<int> result;
16+
stack<TreeNode *> st;
17+
TreeNode *p = root;
18+
19+
while(p || !st.empty()){
20+
if(p){
21+
st.push(p);
22+
p = p->left;
23+
}else{
24+
p = st.top();
25+
st.pop();
26+
27+
result.push_back(p->val);
28+
p = p->right;
29+
}
30+
}
31+
32+
return result;
33+
}
34+
};

Binary_Tree_Maximum_Path_Sum.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Definition for binary tree
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+
11+
class Solution {
12+
public:
13+
int maxPathSum(TreeNode *root) {
14+
int max_sum = INT_MIN;
15+
16+
max_path_sum_helper(root, max_sum);
17+
18+
return max_sum;
19+
}
20+
21+
private:
22+
int max_path_sum_helper(TreeNode *root, int &max_sum){
23+
if(root == nullptr) return 0;
24+
// NOT NEEDED - STILL NEED TO CHECK IF IT IS THE MAX_SUM
25+
// if(root->left == nullptr && root->right == nullptr) return root->val;
26+
27+
//if(root->left == nullptr && root->right == nullptr){
28+
// if(root->val > max_sum) max_sum = root->val;
29+
// return root->val;
30+
//}
31+
32+
int left = max_path_sum_helper(root->left, max_sum);
33+
int right = max_path_sum_helper(root->right, max_sum);
34+
35+
// one option for max path sum
36+
int sum = root->val;
37+
if(left > 0) sum +=left;
38+
if(right > 0) sum += right;
39+
if(sum > max_sum){
40+
max_sum = sum;
41+
}
42+
43+
//max path with the root node included for the upper level
44+
return max(root->val, max(root->val+left, root->val+right));
45+
}
46+
};
47+
48+
/* OBSERVATION:
49+
* Any max path has to go through a 'root' node, so recursivly go
50+
* all the way into the bottom, and the max value will be one of
51+
* the potential path with the internal node.
52+
*/

Binary_Tree_Preorder_Traversal.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for binary tree
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<int> preorderTraversal(TreeNode *root) {
13+
if(root == nullptr) return vector<int>();
14+
15+
vector<int> result;
16+
stack<TreeNode *> st;
17+
st.push(root);
18+
19+
TreeNode *p = nullptr;
20+
while(!st.empty()){
21+
p = st.top();
22+
st.pop();
23+
24+
result.push_back(p->val);
25+
if(p->right) st.push(p->right);
26+
if(p->left) st.push(p->left);
27+
}
28+
29+
return result;
30+
}
31+
};

Climbing_Stairs.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int climbStairs(int n) {
4+
if(n <= 2) return n;
5+
6+
int first = 1;
7+
int second = 2;
8+
int third;
9+
while(n > 2){
10+
third = first+second;
11+
first = second;
12+
second = third;
13+
n--;
14+
}
15+
return second;
16+
}
17+
};
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Definition for binary tree
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+
void flatten(TreeNode *root) {
13+
if(root == nullptr) return;
14+
if(root->left == nullptr && root->right == nullptr) return;
15+
16+
stack<TreeNode *> st;
17+
st.push(root);
18+
TreeNode *p = nullptr;
19+
TreeNode *pre = nullptr;
20+
21+
while(!st.empty()){
22+
p = st.top();
23+
st.pop();
24+
25+
if(pre){
26+
pre->left = nullptr;
27+
pre->right = p;
28+
}
29+
pre = p;
30+
31+
if(p->right) st.push(p->right);
32+
if(p->left) st.push(p->left);
33+
}
34+
35+
pre->left = pre->right = nullptr;
36+
}
37+
};

Four_Sum_Closest.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
vector<vector<int> > fourSum(vector<int> &num, int target) {
4+
vector<vector<int>> solution;
5+
set<vector<int>> unique;
6+
7+
if(num.size() < 4) return solution;
8+
9+
sort(num.begin(), num.end());
10+
11+
for(int i = 0; i < num.size()-3; i++){
12+
for(int j = i+1; j < num.size(); j++){
13+
14+
int p = j+1;
15+
int q = num.size()-1;
16+
17+
while(p < q){
18+
if(num[i]+num[j]+num[p]+num[q] == target){
19+
unique.insert({num[i], num[j], num[p], num[q]});
20+
p++;
21+
q--;
22+
}else if(num[i]+num[j]+num[p]+num[q] > target){
23+
q--;
24+
}else{
25+
p++;
26+
}
27+
}
28+
}
29+
}
30+
31+
for(auto it = unique.begin(); it != unique.end(); ++it){
32+
solution.push_back(*it);
33+
}
34+
35+
return solution;
36+
}
37+
};

LRU_Cache.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class LRUCache{
2+
private:
3+
int size;
4+
int cap;
5+
unordered_map<int, list<pair<int, int>>::iterator> map;
6+
list<pair<int, int>> lst;
7+
8+
void update(int key, int val){
9+
auto it = map[key];
10+
pair<int, int> t = {key, val};
11+
lst.erase(it);
12+
lst.push_front(t);
13+
map[key] = lst.begin();
14+
}
15+
16+
void remove_least(){
17+
pair<int, int> t = lst.back();
18+
lst.pop_back();
19+
map.erase(t.first);
20+
size--;
21+
}
22+
23+
void insert(int key, int val){
24+
lst.push_front({key, val});
25+
map[key] = lst.begin();
26+
size++;
27+
}
28+
29+
public:
30+
LRUCache(int capacity) {
31+
cap = capacity;
32+
size = 0;
33+
}
34+
35+
int get(int key) {
36+
if(map.find(key) == map.end()) return -1;
37+
int val = map[key]->second;
38+
update(key, val);
39+
return val;
40+
}
41+
42+
void set(int key, int value) {
43+
if(map.find(key) != map.end()){
44+
update(key, value);
45+
}else{
46+
if(size == cap){
47+
remove_least();
48+
}
49+
insert(key, value);
50+
}
51+
}
52+
};

Longest_Consecutive_Sequence.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public:
3+
int longestConsecutive(vector<int> &num) {
4+
if(num.empty()) return 0;
5+
if(num.size() == 1) return 1;
6+
7+
unordered_map<int, bool> map;
8+
for(int i = 0; i < num.size(); i++){
9+
map[num[i]] = false;
10+
}
11+
12+
int max_len = INT_MIN;
13+
int len;
14+
int p;
15+
for(int i = 0; i < num.size(); i++){
16+
if(map[num[i]]) continue;
17+
18+
len = 1;
19+
map[num[i]] = true;
20+
21+
// search the increasing direction
22+
p = num[i]+1;
23+
while(map.find(p) != map.end()){
24+
len++;
25+
map[p] = true;
26+
p++;
27+
}
28+
// search the decreasing direction
29+
p = num[i]-1;
30+
while(map.find(p) != map.end()){
31+
len++;
32+
map[p] = true;
33+
p--;
34+
}
35+
36+
if(len > max_len){
37+
max_len = len;
38+
}
39+
}
40+
41+
return max_len;
42+
}
43+
};

Minimum_Depth_of_Binary_Tree.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for binary tree
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 minDepth(TreeNode *root) {
13+
if(root == nullptr) return 0;
14+
15+
if(root->left && root->right) return min(minDepth(root->left), minDepth(root->right))+1;
16+
else if(root->left == nullptr && root->right) return minDepth(root->right)+1;
17+
else if(root->left && root->right == nullptr) return minDepth(root->left)+1;
18+
else return 1;
19+
}
20+
};
21+
22+
/**
23+
* Watch out for internal node with only one child
24+
* There are four cases for a node:
25+
* 1. left and right
26+
* 2. left
27+
* 3. right
28+
* 4. leaf node
29+
*/

Minimum_Path_Sum.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int minPathSum(vector<vector<int> > &grid) {
4+
int m = grid.size();
5+
int n = grid[0].size();
6+
7+
for(int i = 1; i < m; i++){
8+
grid[i][0] += grid[i-1][0];
9+
}
10+
11+
for(int j = 1; j < n; j++){
12+
grid[0][j] += grid[0][j-1];
13+
}
14+
15+
for(int i = 1; i < m; i++){
16+
for(int j = 1; j < n; j++){
17+
grid[i][j] += min(grid[i-1][j], grid[i][j-1]);
18+
}
19+
}
20+
return grid[m-1][n-1];
21+
}
22+
};

0 commit comments

Comments
 (0)