From b724c8164b3399a64ae50a2c44e80aa00855dc8a Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 10 Feb 2026 04:39:29 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_8.py | 16 ++++ .../common_algos/valid_palindrome_round_8.py | 22 +++++ ...x_72_flatten_binary_tree_to_linked_list.py | 36 +++++++ ...ulating_next_right_pointer_in_each_node.ts | 96 ++++++++----------- ...x_72_flatten_binary_tree_to_linked_list.ts | 30 ++++++ ...ten_binary_tree_to_linked_list_round_22.py | 81 ++++++++++++++++ 6 files changed, 226 insertions(+), 55 deletions(-) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_8.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_8.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_72_flatten_binary_tree_to_linked_list.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_72_flatten_binary_tree_to_linked_list.ts create mode 100644 tests/test_150_questions_round_22/test_72_flatten_binary_tree_to_linked_list_round_22.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_8.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_8.py new file mode 100644 index 00000000..6c0c6330 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_8.py @@ -0,0 +1,16 @@ +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 [] \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_8.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_8.py new file mode 100644 index 00000000..cad2b1e6 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_8.py @@ -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 \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_72_flatten_binary_tree_to_linked_list.py b/src/my_project/interviews/top_150_questions_round_22/ex_72_flatten_binary_tree_to_linked_list.py new file mode 100644 index 00000000..e20de18c --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_72_flatten_binary_tree_to_linked_list.py @@ -0,0 +1,36 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + +class Solution: + def flatten(self, root: Optional[TreeNode]) -> None: + """ + Do not return anything, modify root in-place instead. + """ + if not root: + return + + current = root + + while current: + if current.left: + # Find the rightmost node in the left subtree + rightmost = current.left + while rightmost.right: + rightmost = rightmost.right + + # Save the current right subtree + # Connect it to the rightmost node of left subtree + rightmost.right = current.right + + # Move the left subtree to the right + current.right = current.left + current.left = None + + # Move to the next node + current = current.right \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_71_populating_next_right_pointer_in_each_node.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_71_populating_next_right_pointer_in_each_node.ts index 9402db54..804f3d12 100644 --- a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_71_populating_next_right_pointer_in_each_node.ts +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_71_populating_next_right_pointer_in_each_node.ts @@ -1,60 +1,46 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod -from collections import deque +class _Node { + val: number + left: _Node | null + right: _Node | null + next: _Node | null -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 + 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) + } +} -class Solution: - def connect(self, root: 'Node') -> 'Node': - if not root: - return None - - # BFS using queue - queue = deque([root]) +function connect(root: _Node | null): _Node | null { + if (!root) { + return null; + } + + // BFS using queue + const queue: _Node[] = [root]; + + while (queue.length > 0) { + const levelSize = queue.length; - while queue: - level_size = len(queue) + // Process all nodes at current level + for (let i = 0; i < levelSize; i++) { + const node = queue.shift()!; - # 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 { + // Connect to next node in the level (if not the last node) + if (i < levelSize - 1) { + node.next = queue[0]; + } + + // Add children to queue for next level + if (node.left) { + queue.push(node.left); + } + if (node.right) { + queue.push(node.right); + } + } + } -}; \ No newline at end of file + return root; +} diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_72_flatten_binary_tree_to_linked_list.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_72_flatten_binary_tree_to_linked_list.ts new file mode 100644 index 00000000..ceafdce6 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_72_flatten_binary_tree_to_linked_list.ts @@ -0,0 +1,30 @@ +import { TreeNode } from './TreeNode'; + +function flatten(root: TreeNode | null): void { + if (!root) { + return; + } + + let current: TreeNode | null = root; + + while (current) { + if (current.left) { + // Find the rightmost node in the left subtree + let rightmost: TreeNode = current.left; + while (rightmost.right) { + rightmost = rightmost.right; + } + + // Save the current right subtree + // Connect it to the rightmost node of left subtree + rightmost.right = current.right; + + // Move the left subtree to the right + current.right = current.left; + current.left = null; + } + + // Move to the next node + current = current.right; + } +} diff --git a/tests/test_150_questions_round_22/test_72_flatten_binary_tree_to_linked_list_round_22.py b/tests/test_150_questions_round_22/test_72_flatten_binary_tree_to_linked_list_round_22.py new file mode 100644 index 00000000..6ff87970 --- /dev/null +++ b/tests/test_150_questions_round_22/test_72_flatten_binary_tree_to_linked_list_round_22.py @@ -0,0 +1,81 @@ +import unittest +from typing import Optional, List +from src.my_project.interviews.top_150_questions_round_22\ +.ex_72_flatten_binary_tree_to_linked_list import Solution, TreeNode + + +class FlattenBinaryTreeToLinkedListTestCase(unittest.TestCase): + + def build_tree(self, values: List[Optional[int]]) -> Optional[TreeNode]: + """Build tree from level-order list representation.""" + if not values: + return None + + root = TreeNode(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 = TreeNode(values[i]) + queue.append(node.left) + i += 1 + + # Add right child + if i < len(values) and values[i] is not None: + node.right = TreeNode(values[i]) + queue.append(node.right) + i += 1 + + return root + + def tree_to_array(self, root: Optional[TreeNode]) -> List[Optional[int]]: + """Convert flattened tree to array format.""" + result = [] + current = root + + while current: + result.append(current.val) + result.append(current.left) # Should always be None + current = current.right + + return result + + def test_example_1(self): + """ + Input: root = [1,2,5,3,4,null,6] + Output: [1,null,2,null,3,null,4,null,5,null,6] + """ + solution = Solution() + root = self.build_tree([1, 2, 5, 3, 4, None, 6]) + solution.flatten(root) + output = self.tree_to_array(root) + expected = [1, None, 2, None, 3, None, 4, None, 5, None, 6, None] + self.assertEqual(output, expected) + + def test_example_2(self): + """ + Input: root = [] + Output: [] + """ + solution = Solution() + root = self.build_tree([]) + solution.flatten(root) + output = self.tree_to_array(root) + expected = [] + self.assertEqual(output, expected) + + def test_example_3(self): + """ + Input: root = [0] + Output: [0] + """ + solution = Solution() + root = self.build_tree([0]) + solution.flatten(root) + output = self.tree_to_array(root) + expected = [0, None] + self.assertEqual(output, expected) \ No newline at end of file