Skip to content

Commit cffab73

Browse files
authored
Merge pull request #1551 from ivanpenaloza/february17
adding algo
2 parents a64905d + b57b6d9 commit cffab73

6 files changed

Lines changed: 175 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def twoSum(self, nums: List[int], target: int) -> List[int]:
6+
7+
answer = dict()
8+
9+
for k, v in enumerate(nums):
10+
11+
if v in answer:
12+
return [answer[v], k]
13+
else:
14+
answer[target - v] = k
15+
16+
return []
17+
18+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
import re
4+
5+
class Solution:
6+
def isPalindrome(self, s: str) -> bool:
7+
8+
# To lowercase
9+
s = s.lower()
10+
11+
# Remove non-alphanumeric characters
12+
s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s)
13+
14+
# Determine if s is palindrome or not
15+
len_s = len(s)
16+
17+
for i in range(len_s//2):
18+
19+
if s[i] != s[len_s - 1 - i]:
20+
return False
21+
22+
return True
23+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
from collections import deque
4+
5+
class TreeNode:
6+
def __init__(self, val=0, left=None, right=None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
11+
class Solution:
12+
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
13+
if not root:
14+
return []
15+
16+
result = []
17+
queue = deque([root])
18+
19+
while queue:
20+
level_size = len(queue)
21+
22+
for i in range(level_size):
23+
node = queue.popleft()
24+
25+
# Add the rightmost node of each level
26+
if i == level_size - 1:
27+
result.append(node.val)
28+
29+
# Add children to queue (left first, then right)
30+
if node.left:
31+
queue.append(node.left)
32+
if node.right:
33+
queue.append(node.right)
34+
35+
return result

src/my_project/interviews_typescript/top_150_questions_round_1/test_78_lowest_common_ancestor_of_binary_tree_round_22.ts renamed to src/my_project/interviews_typescript/top_150_questions_round_1/ex_78_lowest_common_ancestor_of_binary_tree.ts

File renamed without changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { TreeNode } from './TreeNode';
2+
3+
function rightSideView(root: TreeNode | null): number[] {
4+
if (!root) {
5+
return [];
6+
}
7+
8+
const result: number[] = [];
9+
const queue: TreeNode[] = [root];
10+
11+
while (queue.length > 0) {
12+
const levelSize = queue.length;
13+
14+
for (let i = 0; i < levelSize; i++) {
15+
const node = queue.shift()!;
16+
17+
// Add the rightmost node of each level
18+
if (i === levelSize - 1) {
19+
result.push(node.val);
20+
}
21+
22+
// Add children to queue (left first, then right)
23+
if (node.left) {
24+
queue.push(node.left);
25+
}
26+
if (node.right) {
27+
queue.push(node.right);
28+
}
29+
}
30+
}
31+
32+
return result;
33+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_79_binary_tree_right_side_view import Solution, TreeNode
4+
5+
class BinaryTreeRightSideViewTestCase(unittest.TestCase):
6+
7+
def create_binary_tree(self, values):
8+
"""
9+
Helper function to create a binary tree from a list of values (level-order).
10+
11+
:param values: List of node values (None represents null nodes)
12+
:return: Root of the binary tree
13+
"""
14+
if not values:
15+
return None
16+
17+
root = TreeNode(values[0])
18+
queue = [root]
19+
i = 1
20+
21+
while queue and i < len(values):
22+
node = queue.pop(0)
23+
24+
if i < len(values) and values[i] is not None:
25+
node.left = TreeNode(values[i])
26+
queue.append(node.left)
27+
i += 1
28+
29+
if i < len(values) and values[i] is not None:
30+
node.right = TreeNode(values[i])
31+
queue.append(node.right)
32+
i += 1
33+
34+
return root
35+
36+
def test_example_1(self):
37+
# Example 1: Input: root = [1,2,3,null,5,null,4]
38+
# Output: [1,3,4]
39+
solution = Solution()
40+
root = self.create_binary_tree([1, 2, 3, None, 5, None, 4])
41+
result = solution.rightSideView(root)
42+
self.assertEqual(result, [1, 3, 4])
43+
44+
def test_example_2(self):
45+
# Example 2: Input: root = [1,2,3,4,null,null,null,5]
46+
# Output: [1,3,4,5]
47+
solution = Solution()
48+
root = self.create_binary_tree([1, 2, 3, 4, None, None, None, 5])
49+
result = solution.rightSideView(root)
50+
self.assertEqual(result, [1, 3, 4, 5])
51+
52+
def test_example_3(self):
53+
# Example 3: Input: root = [1,null,3]
54+
# Output: [1,3]
55+
solution = Solution()
56+
root = self.create_binary_tree([1, None, 3])
57+
result = solution.rightSideView(root)
58+
self.assertEqual(result, [1, 3])
59+
60+
def test_example_4(self):
61+
# Example 4: Input: root = []
62+
# Output: []
63+
solution = Solution()
64+
root = self.create_binary_tree([])
65+
result = solution.rightSideView(root)
66+
self.assertEqual(result, [])

0 commit comments

Comments
 (0)