Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:

answer = dict()

for k, v in enumerate(nums):
if v in answer:
return [answer[v], k]
else:
answer[target - v] = k

return []
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod
import re

class Solution:
def isPalindrome(self, s: str) -> bool:

# To lowercase
s = s.lower()

# Remove non-alphanumeric characters
s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s)

# Determine if s is palindrome or not
len_s = len(s)

for i in range(len_s//2):

if s[i] != s[len_s - 1 - i]:
return False

return True
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod
from collections import deque

class Node:
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
self.val = val
self.left = left
self.right = right
self.next = next

class Solution:
def connect(self, root: 'Node') -> 'Node':
if not root:
return None

# BFS using queue
queue = deque([root])

while queue:
level_size = len(queue)

# Process all nodes at current level
for i in range(level_size):
node = queue.popleft()

# Connect to next node in the level (if not the last node)
if i < level_size - 1:
node.next = queue[0]

# Add children to queue for next level
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)

return root
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod
from collections import deque

class Node:
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
self.val = val
self.left = left
self.right = right
self.next = next

class Solution:
def connect(self, root: 'Node') -> 'Node':
if not root:
return None

# BFS using queue
queue = deque([root])

while queue:
level_size = len(queue)

# Process all nodes at current level
for i in range(level_size):
node = queue.popleft()

# Connect to next node in the level (if not the last node)
if i < level_size - 1:
node.next = queue[0]

# Add children to queue for next level
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)

return root


/**
* Definition for _Node.
* class _Node {
* val: number
* left: _Node | null
* right: _Node | null
* next: _Node | null
*
* constructor(val?: number, left?: _Node, right?: _Node, next?: _Node) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* this.next = (next===undefined ? null : next)
* }
* }
*/


function connect(root: _Node | null): _Node | null {

};
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import unittest
from typing import Optional, List
from src.my_project.interviews.top_150_questions_round_22\
.ex_71_populating_next_right_pointer_in_each_node import Solution, Node

class PopulatingNextRightPointersInEachNodeTestCase(unittest.TestCase):

def build_tree(self, values: List[Optional[int]]) -> Optional[Node]:
"""Build tree from level-order list representation."""
if not values:
return None

root = Node(values[0])
queue = [root]
i = 1

while queue and i < len(values):
node = queue.pop(0)

# Add left child
if i < len(values) and values[i] is not None:
node.left = Node(values[i])
queue.append(node.left)
i += 1

# Add right child
if i < len(values) and values[i] is not None:
node.right = Node(values[i])
queue.append(node.right)
i += 1

return root

def verify_next_pointers(self, root: Optional[Node]) -> List[str]:
"""Verify next pointers and return serialized output."""
if not root:
return []

result = []
leftmost = root

while leftmost:
current = leftmost
while current:
result.append(str(current.val))
current = current.next
result.append('#')

# Move to next level
leftmost = leftmost.left if leftmost.left else leftmost.right
if not leftmost:
# Find the first node in the next level
temp = root
queue = [root]
while queue:
node = queue.pop(0)
if node.left and node.left not in [n for level in [root] for n in [root]]:
leftmost = node.left
break
if node.right:
leftmost = node.right
break
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
break

return result

def get_level_order_with_next(self, root: Optional[Node]) -> str:
"""Get level order traversal following next pointers."""
if not root:
return ""

result = []
leftmost = root

while leftmost:
current = leftmost
level_vals = []
while current:
level_vals.append(str(current.val))
current = current.next
result.extend(level_vals)
result.append('#')

# Find leftmost node of next level
temp = leftmost
leftmost = None
while temp and not leftmost:
if temp.left:
leftmost = temp.left
elif temp.right:
leftmost = temp.right
else:
temp = temp.next

return ','.join(result)

def test_example_1(self):
"""
Input: root = [1,2,3,4,5,null,7]
Output: [1,#,2,3,#,4,5,7,#]
"""
solution = Solution()
root = self.build_tree([1, 2, 3, 4, 5, None, 7])
result = solution.connect(root)
output = self.get_level_order_with_next(result)
expected = "1,#,2,3,#,4,5,7,#"
self.assertEqual(output, expected)

def test_example_2(self):
"""
Input: root = []
Output: []
"""
solution = Solution()
root = self.build_tree([])
result = solution.connect(root)
output = self.get_level_order_with_next(result)
expected = ""
self.assertEqual(output, expected)