Skip to content

Commit 90e9d56

Browse files
authored
Merge pull request #1543 from ivanpenaloza/february09
adding algo
2 parents c020afb + 6bcb196 commit 90e9d56

5 files changed

Lines changed: 257 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
if v in answer:
11+
return [answer[v], k]
12+
else:
13+
answer[target - v] = k
14+
15+
return []
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
from collections import deque
4+
5+
class Node:
6+
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
self.next = next
11+
12+
class Solution:
13+
def connect(self, root: 'Node') -> 'Node':
14+
if not root:
15+
return None
16+
17+
# BFS using queue
18+
queue = deque([root])
19+
20+
while queue:
21+
level_size = len(queue)
22+
23+
# Process all nodes at current level
24+
for i in range(level_size):
25+
node = queue.popleft()
26+
27+
# Connect to next node in the level (if not the last node)
28+
if i < level_size - 1:
29+
node.next = queue[0]
30+
31+
# Add children to queue for next level
32+
if node.left:
33+
queue.append(node.left)
34+
if node.right:
35+
queue.append(node.right)
36+
37+
return root
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
from collections import deque
4+
5+
class Node:
6+
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
self.next = next
11+
12+
class Solution:
13+
def connect(self, root: 'Node') -> 'Node':
14+
if not root:
15+
return None
16+
17+
# BFS using queue
18+
queue = deque([root])
19+
20+
while queue:
21+
level_size = len(queue)
22+
23+
# Process all nodes at current level
24+
for i in range(level_size):
25+
node = queue.popleft()
26+
27+
# Connect to next node in the level (if not the last node)
28+
if i < level_size - 1:
29+
node.next = queue[0]
30+
31+
# Add children to queue for next level
32+
if node.left:
33+
queue.append(node.left)
34+
if node.right:
35+
queue.append(node.right)
36+
37+
return root
38+
39+
40+
/**
41+
* Definition for _Node.
42+
* class _Node {
43+
* val: number
44+
* left: _Node | null
45+
* right: _Node | null
46+
* next: _Node | null
47+
*
48+
* constructor(val?: number, left?: _Node, right?: _Node, next?: _Node) {
49+
* this.val = (val===undefined ? 0 : val)
50+
* this.left = (left===undefined ? null : left)
51+
* this.right = (right===undefined ? null : right)
52+
* this.next = (next===undefined ? null : next)
53+
* }
54+
* }
55+
*/
56+
57+
58+
function connect(root: _Node | null): _Node | null {
59+
60+
};
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import unittest
2+
from typing import Optional, List
3+
from src.my_project.interviews.top_150_questions_round_22\
4+
.ex_71_populating_next_right_pointer_in_each_node import Solution, Node
5+
6+
class PopulatingNextRightPointersInEachNodeTestCase(unittest.TestCase):
7+
8+
def build_tree(self, values: List[Optional[int]]) -> Optional[Node]:
9+
"""Build tree from level-order list representation."""
10+
if not values:
11+
return None
12+
13+
root = Node(values[0])
14+
queue = [root]
15+
i = 1
16+
17+
while queue and i < len(values):
18+
node = queue.pop(0)
19+
20+
# Add left child
21+
if i < len(values) and values[i] is not None:
22+
node.left = Node(values[i])
23+
queue.append(node.left)
24+
i += 1
25+
26+
# Add right child
27+
if i < len(values) and values[i] is not None:
28+
node.right = Node(values[i])
29+
queue.append(node.right)
30+
i += 1
31+
32+
return root
33+
34+
def verify_next_pointers(self, root: Optional[Node]) -> List[str]:
35+
"""Verify next pointers and return serialized output."""
36+
if not root:
37+
return []
38+
39+
result = []
40+
leftmost = root
41+
42+
while leftmost:
43+
current = leftmost
44+
while current:
45+
result.append(str(current.val))
46+
current = current.next
47+
result.append('#')
48+
49+
# Move to next level
50+
leftmost = leftmost.left if leftmost.left else leftmost.right
51+
if not leftmost:
52+
# Find the first node in the next level
53+
temp = root
54+
queue = [root]
55+
while queue:
56+
node = queue.pop(0)
57+
if node.left and node.left not in [n for level in [root] for n in [root]]:
58+
leftmost = node.left
59+
break
60+
if node.right:
61+
leftmost = node.right
62+
break
63+
if node.left:
64+
queue.append(node.left)
65+
if node.right:
66+
queue.append(node.right)
67+
break
68+
69+
return result
70+
71+
def get_level_order_with_next(self, root: Optional[Node]) -> str:
72+
"""Get level order traversal following next pointers."""
73+
if not root:
74+
return ""
75+
76+
result = []
77+
leftmost = root
78+
79+
while leftmost:
80+
current = leftmost
81+
level_vals = []
82+
while current:
83+
level_vals.append(str(current.val))
84+
current = current.next
85+
result.extend(level_vals)
86+
result.append('#')
87+
88+
# Find leftmost node of next level
89+
temp = leftmost
90+
leftmost = None
91+
while temp and not leftmost:
92+
if temp.left:
93+
leftmost = temp.left
94+
elif temp.right:
95+
leftmost = temp.right
96+
else:
97+
temp = temp.next
98+
99+
return ','.join(result)
100+
101+
def test_example_1(self):
102+
"""
103+
Input: root = [1,2,3,4,5,null,7]
104+
Output: [1,#,2,3,#,4,5,7,#]
105+
"""
106+
solution = Solution()
107+
root = self.build_tree([1, 2, 3, 4, 5, None, 7])
108+
result = solution.connect(root)
109+
output = self.get_level_order_with_next(result)
110+
expected = "1,#,2,3,#,4,5,7,#"
111+
self.assertEqual(output, expected)
112+
113+
def test_example_2(self):
114+
"""
115+
Input: root = []
116+
Output: []
117+
"""
118+
solution = Solution()
119+
root = self.build_tree([])
120+
result = solution.connect(root)
121+
output = self.get_level_order_with_next(result)
122+
expected = ""
123+
self.assertEqual(output, expected)

0 commit comments

Comments
 (0)