Skip to content

Commit baf3f6a

Browse files
🌲 Day 6
1 parent 3b8301e commit baf3f6a

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
1. [Number of Recent Calls](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/559/week-1-october-1st-october-7th/3480/) ➡️ [CPP Solution](Week1/RecentCounter.cpp)
1010
2. [Combination Sum](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/559/week-1-october-1st-october-7th/3481/) ➡️ [CPP Solution](Week1/combinationSum.cpp)
1111
3. [K-diff Pairs in an Array](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/559/week-1-october-1st-october-7th/3482/) ➡️ [CPP Solution](Week1/findPairs.cpp)
12-
4. [Remove Covered Intervals](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/559/week-1-october-1st-october-7th/3482/) ➡️ [CPP Solution](Week1/removeCoveredIntervals.cpp)
13-
5. [Complement of Base 10 Integer](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/559/week-1-october-1st-october-7th/3482/) ➡️ [CPP Solution](Week1/bitwiseComplement.cpp)
12+
4. [Remove Covered Intervals](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/559/week-1-october-1st-october-7th/3483/) ➡️ [CPP Solution](Week1/removeCoveredIntervals.cpp)
13+
5. [Complement of Base 10 Integer](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/559/week-1-october-1st-october-7th/3484/) ➡️ [CPP Solution](Week1/bitwiseComplement.cpp)
14+
6. [Insert into a Binary Search Tree](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/559/week-1-october-1st-october-7th/3485/) ➡️ [CPP Solution](Week1/insertIntoBST.cpp)
1415

1516
## Week 2 🚧
1617

Week1/insertIntoBST.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
13+
/*
14+
Recursive Approach
15+
*/
16+
class Solution {
17+
public:
18+
TreeNode* insertIntoBST(TreeNode* root, int val) {
19+
if(root == NULL) return new TreeNode(val);
20+
21+
if(val > root->val)
22+
root->right = insertIntoBST(root->right, val);
23+
else if(val < root->val)
24+
root->left = insertIntoBST(root->left, val);
25+
26+
return root;
27+
}
28+
};
29+
30+
/*
31+
Iterative Approach
32+
*/
33+
class Solution {
34+
public:
35+
TreeNode* insertIntoBST(TreeNode* root, int val) {
36+
if(root == NULL) return new TreeNode(val);
37+
38+
TreeNode* current = root;
39+
40+
while(true) {
41+
if(val > current->val) {
42+
if(current->right != NULL)
43+
current = current->right;
44+
else {
45+
current->right = new TreeNode(val);
46+
break;
47+
}
48+
} else {
49+
if(current->left != NULL)
50+
current = current->left;
51+
else {
52+
current->left = new TreeNode(val);
53+
break;
54+
}
55+
}
56+
}
57+
58+
return root;
59+
}
60+
};

0 commit comments

Comments
 (0)