Skip to content

Commit d8ef3be

Browse files
authored
Merge pull request #1544 from ivanpenaloza/february10
adding algo
2 parents 90e9d56 + b724c81 commit d8ef3be

6 files changed

Lines changed: 226 additions & 55 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 []
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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class TreeNode:
5+
def __init__(self, val=0, left=None, right=None):
6+
self.val = val
7+
self.left = left
8+
self.right = right
9+
10+
class Solution:
11+
def flatten(self, root: Optional[TreeNode]) -> None:
12+
"""
13+
Do not return anything, modify root in-place instead.
14+
"""
15+
if not root:
16+
return
17+
18+
current = root
19+
20+
while current:
21+
if current.left:
22+
# Find the rightmost node in the left subtree
23+
rightmost = current.left
24+
while rightmost.right:
25+
rightmost = rightmost.right
26+
27+
# Save the current right subtree
28+
# Connect it to the rightmost node of left subtree
29+
rightmost.right = current.right
30+
31+
# Move the left subtree to the right
32+
current.right = current.left
33+
current.left = None
34+
35+
# Move to the next node
36+
current = current.right
Lines changed: 41 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,46 @@
1-
from typing import List, Union, Collection, Mapping, Optional
2-
from abc import ABC, abstractmethod
3-
from collections import deque
1+
class _Node {
2+
val: number
3+
left: _Node | null
4+
right: _Node | null
5+
next: _Node | null
46

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
7+
constructor(val?: number, left?: _Node, right?: _Node, next?: _Node) {
8+
this.val = (val===undefined ? 0 : val)
9+
this.left = (left===undefined ? null : left)
10+
this.right = (right===undefined ? null : right)
11+
this.next = (next===undefined ? null : next)
12+
}
13+
}
1114

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])
15+
function connect(root: _Node | null): _Node | null {
16+
if (!root) {
17+
return null;
18+
}
19+
20+
// BFS using queue
21+
const queue: _Node[] = [root];
22+
23+
while (queue.length > 0) {
24+
const levelSize = queue.length;
1925

20-
while queue:
21-
level_size = len(queue)
26+
// Process all nodes at current level
27+
for (let i = 0; i < levelSize; i++) {
28+
const node = queue.shift()!;
2229

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 {
30+
// Connect to next node in the level (if not the last node)
31+
if (i < levelSize - 1) {
32+
node.next = queue[0];
33+
}
34+
35+
// Add children to queue for next level
36+
if (node.left) {
37+
queue.push(node.left);
38+
}
39+
if (node.right) {
40+
queue.push(node.right);
41+
}
42+
}
43+
}
5944

60-
};
45+
return root;
46+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { TreeNode } from './TreeNode';
2+
3+
function flatten(root: TreeNode | null): void {
4+
if (!root) {
5+
return;
6+
}
7+
8+
let current: TreeNode | null = root;
9+
10+
while (current) {
11+
if (current.left) {
12+
// Find the rightmost node in the left subtree
13+
let rightmost: TreeNode = current.left;
14+
while (rightmost.right) {
15+
rightmost = rightmost.right;
16+
}
17+
18+
// Save the current right subtree
19+
// Connect it to the rightmost node of left subtree
20+
rightmost.right = current.right;
21+
22+
// Move the left subtree to the right
23+
current.right = current.left;
24+
current.left = null;
25+
}
26+
27+
// Move to the next node
28+
current = current.right;
29+
}
30+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import unittest
2+
from typing import Optional, List
3+
from src.my_project.interviews.top_150_questions_round_22\
4+
.ex_72_flatten_binary_tree_to_linked_list import Solution, TreeNode
5+
6+
7+
class FlattenBinaryTreeToLinkedListTestCase(unittest.TestCase):
8+
9+
def build_tree(self, values: List[Optional[int]]) -> Optional[TreeNode]:
10+
"""Build tree from level-order list representation."""
11+
if not values:
12+
return None
13+
14+
root = TreeNode(values[0])
15+
queue = [root]
16+
i = 1
17+
18+
while queue and i < len(values):
19+
node = queue.pop(0)
20+
21+
# Add left child
22+
if i < len(values) and values[i] is not None:
23+
node.left = TreeNode(values[i])
24+
queue.append(node.left)
25+
i += 1
26+
27+
# Add right child
28+
if i < len(values) and values[i] is not None:
29+
node.right = TreeNode(values[i])
30+
queue.append(node.right)
31+
i += 1
32+
33+
return root
34+
35+
def tree_to_array(self, root: Optional[TreeNode]) -> List[Optional[int]]:
36+
"""Convert flattened tree to array format."""
37+
result = []
38+
current = root
39+
40+
while current:
41+
result.append(current.val)
42+
result.append(current.left) # Should always be None
43+
current = current.right
44+
45+
return result
46+
47+
def test_example_1(self):
48+
"""
49+
Input: root = [1,2,5,3,4,null,6]
50+
Output: [1,null,2,null,3,null,4,null,5,null,6]
51+
"""
52+
solution = Solution()
53+
root = self.build_tree([1, 2, 5, 3, 4, None, 6])
54+
solution.flatten(root)
55+
output = self.tree_to_array(root)
56+
expected = [1, None, 2, None, 3, None, 4, None, 5, None, 6, None]
57+
self.assertEqual(output, expected)
58+
59+
def test_example_2(self):
60+
"""
61+
Input: root = []
62+
Output: []
63+
"""
64+
solution = Solution()
65+
root = self.build_tree([])
66+
solution.flatten(root)
67+
output = self.tree_to_array(root)
68+
expected = []
69+
self.assertEqual(output, expected)
70+
71+
def test_example_3(self):
72+
"""
73+
Input: root = [0]
74+
Output: [0]
75+
"""
76+
solution = Solution()
77+
root = self.build_tree([0])
78+
solution.flatten(root)
79+
output = self.tree_to_array(root)
80+
expected = [0, None]
81+
self.assertEqual(output, expected)

0 commit comments

Comments
 (0)