Skip to content

Commit dd79611

Browse files
committed
Neetcode 150 additional questions
1 parent 4f9af07 commit dd79611

24 files changed

+602
-15
lines changed

.Readme Updater

README.md

+8-2
Large diffs are not rendered by default.

markdowns/Questions_By_Code_Length.md

+6
Large diffs are not rendered by default.

markdowns/Questions_By_Recent.md

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ Calculations are based on the date of the first solve.
66

77
| # | Title | Level | Cats | Solution | Languages | Date Complete |
88
|-----:|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:--------|:---------------|:---------------------------------------------------------------------------------------------------|:---------------------------------|:----------------|
9+
| 124 | [Binary Tree Maximum Path Sum](<https://leetcode.com/problems/binary-tree-maximum-path-sum>) | Hard | B75, N150 | [solution](<_124. Binary Tree Maximum Path Sum.md>) | py | Jul 04, 2024 |
10+
| 1448 | [Count Good Nodes in Binary Tree](<https://leetcode.com/problems/count-good-nodes-in-binary-tree>) | Medium | N150 | [solution](<_1448. Count Good Nodes in Binary Tree.md>) | java | Jul 04, 2024 |
11+
| 199 | [Binary Tree Right Side View](<https://leetcode.com/problems/binary-tree-right-side-view>) | Medium | N150 | [solution](<_199. Binary Tree Right Side View.md>) | java, cpp | Jul 04, 2024 |
12+
| 235 | [Lowest Common Ancestor of a Binary Search Tree](<https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree>) | Medium | B75, N150 | [solution](<_235. Lowest Common Ancestor of a Binary Search Tree.md>) | py | Jul 04, 2024 |
13+
| 110 | [Balanced Binary Tree](<https://leetcode.com/problems/balanced-binary-tree>) | Easy | N150 | [solution](<_110. Balanced Binary Tree.md>) | py | Jul 04, 2024 |
14+
| 104 | [Maximum Depth of Binary Tree](<https://leetcode.com/problems/maximum-depth-of-binary-tree>) | Easy | B75, N150 | [solution](<_104. Maximum Depth of Binary Tree.md>) | py | Jul 04, 2024 |
915
| 82 | [Remove Duplicates from Sorted List II](<https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii>) | Medium | | [solution](<_82. Remove Duplicates from Sorted List II.md>) | py | Jul 04, 2024 |
1016
| 1836 | [Remove Duplicates From an Unsorted Linked List](<https://leetcode.com/problems/remove-duplicates-from-an-unsorted-linked-list>) | Medium | | [solution](<_1836. Remove Duplicates From an Unsorted Linked List.md>) | py | Jul 04, 2024 |
1117
| 2046 | [Sort Linked List Already Sorted Using Absolute Values](<https://leetcode.com/problems/sort-linked-list-already-sorted-using-absolute-values>) | Medium | | [solution](<_2046. Sort Linked List Already Sorted Using Absolute Values.md>) | py | Jul 04, 2024 |

markdowns/Topics.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
- [Array](<by_topic/Array.md>) (171 completed)
88
- [Hash Table](<by_topic/Hash Table.md>) (83 completed)
99
- [String](<by_topic/String.md>) (83 completed)
10-
- [Depth-First Search](<by_topic/Depth-First Search.md>) (59 completed)
11-
- [Tree](<by_topic/Tree.md>) (54 completed)
10+
- [Depth-First Search](<by_topic/Depth-First Search.md>) (65 completed)
11+
- [Tree](<by_topic/Tree.md>) (60 completed)
12+
- [Binary Tree](<by_topic/Binary Tree.md>) (55 completed)
1213
- [Sorting](<by_topic/Sorting.md>) (54 completed)
13-
- [Binary Tree](<by_topic/Binary Tree.md>) (49 completed)
1414
- [Math](<by_topic/Math.md>) (46 completed)
15+
- [Breadth-First Search](<by_topic/Breadth-First Search.md>) (43 completed)
1516
- [Two Pointers](<by_topic/Two Pointers.md>) (41 completed)
16-
- [Breadth-First Search](<by_topic/Breadth-First Search.md>) (40 completed)
1717
- [Linked List](<by_topic/Linked List.md>) (37 completed)
1818
- [Stack](<by_topic/Stack.md>) (34 completed)
1919
- [Greedy](<by_topic/Greedy.md>) (30 completed)
@@ -24,12 +24,12 @@
2424
- [Trie](<by_topic/Trie.md>) (18 completed)
2525
- [Binary Search](<by_topic/Binary Search.md>) (17 completed)
2626
- [Design](<by_topic/Design.md>) (17 completed)
27-
- [Dynamic Programming](<by_topic/Dynamic Programming.md>) (15 completed)
27+
- [Dynamic Programming](<by_topic/Dynamic Programming.md>) (16 completed)
2828
- [Heap (Priority Queue)](<by_topic/Heap (Priority Queue).md>) (15 completed)
2929
- [Recursion](<by_topic/Recursion.md>) (14 completed)
3030
- [Backtracking](<by_topic/Backtracking.md>) (13 completed)
3131
- [Counting](<by_topic/Counting.md>) (13 completed)
32-
- [Binary Search Tree](<by_topic/Binary Search Tree.md>) (11 completed)
32+
- [Binary Search Tree](<by_topic/Binary Search Tree.md>) (12 completed)
3333
- [Prefix Sum](<by_topic/Prefix Sum.md>) (10 completed)
3434
- [Sliding Window](<by_topic/Sliding Window.md>) (10 completed)
3535
- [Queue](<by_topic/Queue.md>) (8 completed)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# 104. [Maximum Depth of Binary Tree](<https://leetcode.com/problems/maximum-depth-of-binary-tree>)
2+
3+
*[Back to top](<../README.md>)*
4+
5+
------
6+
7+
> *First completed : July 04, 2024*
8+
>
9+
> *Last updated : July 04, 2024*
10+
11+
12+
------
13+
14+
> **Related Topics** : **Tree, Depth-First Search, Breadth-First Search, Binary Tree**
15+
>
16+
> **Acceptance Rate** : **75.538 %**
17+
18+
19+
------
20+
21+
*To see the question prompt, click the title.*
22+
23+
## Solutions
24+
25+
- [e104.py](<../my-submissions/e104.py>)
26+
### Python
27+
#### [e104.py](<../my-submissions/e104.py>)
28+
```Python
29+
# Definition for a binary tree node.
30+
# class TreeNode:
31+
# def __init__(self, val=0, left=None, right=None):
32+
# self.val = val
33+
# self.left = left
34+
# self.right = right
35+
class Solution:
36+
def maxDepth(self, root: Optional[TreeNode]) -> int:
37+
if not root :
38+
return 0
39+
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
40+
```
41+
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 110. [Balanced Binary Tree](<https://leetcode.com/problems/balanced-binary-tree>)
2+
3+
*[Back to top](<../README.md>)*
4+
5+
------
6+
7+
> *First completed : July 04, 2024*
8+
>
9+
> *Last updated : July 04, 2024*
10+
11+
12+
------
13+
14+
> **Related Topics** : **Tree, Depth-First Search, Binary Tree**
15+
>
16+
> **Acceptance Rate** : **52.384 %**
17+
18+
19+
------
20+
21+
*To see the question prompt, click the title.*
22+
23+
## Solutions
24+
25+
- [e110.py](<../my-submissions/e110.py>)
26+
### Python
27+
#### [e110.py](<../my-submissions/e110.py>)
28+
```Python
29+
# Definition for a binary tree node.
30+
# class TreeNode:
31+
# def __init__(self, val=0, left=None, right=None):
32+
# self.val = val
33+
# self.left = left
34+
# self.right = right
35+
class Solution:
36+
def isBalanced(self, root: Optional[TreeNode]) -> bool:
37+
def dfs(curr: Optional[TreeNode]) -> int :
38+
if not curr :
39+
return 0
40+
41+
left, right = dfs(curr.left), dfs(curr.right)
42+
43+
if left == -1 or right == -1 :
44+
return -1
45+
46+
if abs(left - right) > 1 :
47+
return -1
48+
49+
return max(left, right) + 1
50+
51+
return True if dfs(root) != -1 else False
52+
```
53+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# 124. [Binary Tree Maximum Path Sum](<https://leetcode.com/problems/binary-tree-maximum-path-sum>)
2+
3+
*[Back to top](<../README.md>)*
4+
5+
------
6+
7+
> *First completed : July 04, 2024*
8+
>
9+
> *Last updated : July 04, 2024*
10+
11+
12+
------
13+
14+
> **Related Topics** : **Dynamic Programming, Tree, Depth-First Search, Binary Tree**
15+
>
16+
> **Acceptance Rate** : **40.121 %**
17+
18+
19+
------
20+
21+
*To see the question prompt, click the title.*
22+
23+
## Solutions
24+
25+
- [h124.py](<../my-submissions/h124.py>)
26+
### Python
27+
#### [h124.py](<../my-submissions/h124.py>)
28+
```Python
29+
# Definition for a binary tree node.
30+
# class TreeNode:
31+
# def __init__(self, val=0, left=None, right=None):
32+
# self.val = val
33+
# self.left = left
34+
# self.right = right
35+
class Solution:
36+
def maxPathSum(self, root: Optional[TreeNode]) -> int:
37+
# Schema: (longest independent, longestChain)
38+
def helper(curr: Optional[TreeNode]) -> Tuple[int, int] :
39+
if not curr :
40+
return (None, None)
41+
if not curr.left and not curr.right :
42+
return (curr.val, curr.val)
43+
44+
leftInd, leftChn = helper(curr.left)
45+
rightInd, rightChn = helper(curr.right)
46+
47+
independentCandidates = [curr.val]
48+
chainCandidates = [0]
49+
if leftInd != None :
50+
independentCandidates.append(leftInd)
51+
independentCandidates[0] += max(0, leftChn)
52+
chainCandidates.append(leftChn)
53+
if rightInd != None :
54+
independentCandidates.append(rightInd)
55+
independentCandidates[0] += max(0, rightChn)
56+
chainCandidates.append(rightChn)
57+
58+
return (max(independentCandidates), max(chainCandidates) + curr.val)
59+
return max(helper(root))
60+
```
61+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# 1448. [Count Good Nodes in Binary Tree](<https://leetcode.com/problems/count-good-nodes-in-binary-tree>)
2+
3+
*[Back to top](<../README.md>)*
4+
5+
------
6+
7+
> *First completed : July 04, 2024*
8+
>
9+
> *Last updated : July 04, 2024*
10+
11+
12+
------
13+
14+
> **Related Topics** : **Tree, Depth-First Search, Breadth-First Search, Binary Tree**
15+
>
16+
> **Acceptance Rate** : **73.019 %**
17+
18+
19+
------
20+
21+
*To see the question prompt, click the title.*
22+
23+
## Solutions
24+
25+
- [m1448.java](<../my-submissions/m1448.java>)
26+
### Java
27+
#### [m1448.java](<../my-submissions/m1448.java>)
28+
```Java
29+
/**
30+
* Definition for a binary tree node.
31+
* public class TreeNode {
32+
* int val;
33+
* TreeNode left;
34+
* TreeNode right;
35+
* TreeNode() {}
36+
* TreeNode(int val) { this.val = val; }
37+
* TreeNode(int val, TreeNode left, TreeNode right) {
38+
* this.val = val;
39+
* this.left = left;
40+
* this.right = right;
41+
* }
42+
* }
43+
*/
44+
class Solution {
45+
public int goodNodes(TreeNode root) {
46+
return goodNodesHelper(root, Integer.MIN_VALUE);
47+
}
48+
private int goodNodesHelper(TreeNode curr, int maxPrevious) {
49+
int output = (curr.val >= maxPrevious) ? 1 : 0;
50+
int maxx = Integer.max(curr.val, maxPrevious);
51+
if (curr.left != null) {
52+
output += goodNodesHelper(curr.left, maxx);
53+
}
54+
if (curr.right != null) {
55+
output += goodNodesHelper(curr.right, maxx);
56+
}
57+
return output;
58+
}
59+
}
60+
```
61+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# 199. [Binary Tree Right Side View](<https://leetcode.com/problems/binary-tree-right-side-view>)
2+
3+
*[Back to top](<../README.md>)*
4+
5+
------
6+
7+
> *First completed : July 04, 2024*
8+
>
9+
> *Last updated : July 04, 2024*
10+
11+
12+
------
13+
14+
> **Related Topics** : **Tree, Depth-First Search, Breadth-First Search, Binary Tree**
15+
>
16+
> **Acceptance Rate** : **63.274 %**
17+
18+
19+
------
20+
21+
*To see the question prompt, click the title.*
22+
23+
## Solutions
24+
25+
- [m199.cpp](<../my-submissions/m199.cpp>)
26+
- [m199.java](<../my-submissions/m199.java>)
27+
### C++
28+
#### [m199.cpp](<../my-submissions/m199.cpp>)
29+
```C++
30+
/**
31+
* Definition for a binary tree node.
32+
* struct TreeNode {
33+
* int val;
34+
* TreeNode *left;
35+
* TreeNode *right;
36+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
37+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
38+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
39+
* };
40+
*/
41+
class Solution {
42+
public:
43+
vector<int> rightSideView(TreeNode* root) {
44+
vector<int> output;
45+
rsvHelper(root, 0, &output);
46+
return output;
47+
}
48+
49+
void rsvHelper(TreeNode* curr, int depth, vector<int>* output) {
50+
if (not curr)
51+
return;
52+
if (depth == output->size())
53+
output->push_back(curr->val);
54+
55+
depth++;
56+
rsvHelper(curr->right, depth, output);
57+
rsvHelper(curr->left, depth, output);
58+
}
59+
};
60+
```
61+
62+
### Java
63+
#### [m199.java](<../my-submissions/m199.java>)
64+
```Java
65+
/**
66+
* Definition for a binary tree node.
67+
* public class TreeNode {
68+
* int val;
69+
* TreeNode left;
70+
* TreeNode right;
71+
* TreeNode() {}
72+
* TreeNode(int val) { this.val = val; }
73+
* TreeNode(int val, TreeNode left, TreeNode right) {
74+
* this.val = val;
75+
* this.left = left;
76+
* this.right = right;
77+
* }
78+
* }
79+
*/
80+
class Solution {
81+
public List<Integer> rightSideView(TreeNode root) {
82+
List<Integer> output = new LinkedList<>();
83+
rsvHelper(root, 0, output);
84+
return output;
85+
}
86+
87+
private void rsvHelper(TreeNode curr, int depth, List<Integer> output) {
88+
if (curr == null) {
89+
return;
90+
}
91+
if (depth == output.size()) {
92+
output.add(curr.val);
93+
}
94+
95+
depth++;
96+
rsvHelper(curr.right, depth, output);
97+
rsvHelper(curr.left, depth, output);
98+
}
99+
}
100+
```
101+

0 commit comments

Comments
 (0)