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,18 @@
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,23 @@
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,28 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod
from collections import deque

class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right

class Solution:
def isValidBST(self, root: Optional[TreeNode]) -> bool:
def validate(node: Optional[TreeNode], min_val: float, max_val: float) -> bool:
# Empty tree is valid
if not node:
return True

# Check if current node violates BST property
if node.val <= min_val or node.val >= max_val:
return False

# Recursively validate left and right subtrees
# Left subtree: all values must be < node.val
# Right subtree: all values must be > node.val
return (validate(node.left, min_val, node.val) and
validate(node.right, node.val, max_val))

return validate(root, float('-inf'), float('inf'))
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { TreeNode } from './TreeNode';

function isValidBST(root: TreeNode | null): boolean {
function validate(node: TreeNode | null, minVal: number, maxVal: number): boolean {
// Empty tree is valid
if (!node) {
return true;
}

// Check if current node violates BST property
if (node.val <= minVal || node.val >= maxVal) {
return false;
}

// Recursively validate left and right subtrees
// Left subtree: all values must be < node.val
// Right subtree: all values must be > node.val
return validate(node.left, minVal, node.val) &&
validate(node.right, node.val, maxVal);
}

return validate(root, -Infinity, Infinity);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import unittest
from src.my_project.interviews.top_150_questions_round_22\
.ex_83_binary_search_tree import Solution, TreeNode

class BinarySearchTreeTestCase(unittest.TestCase):

def create_binary_tree(self, values):
"""
Helper function to create a binary tree from a list of values (level-order).

:param values: List of node values (None represents null nodes)
:return: Root of the binary tree
"""
if not values:
return None

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

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

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

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

return root

def test_example_1(self):
# Example 1: Input: root = [2,1,3]
# Output: true
solution = Solution()
root = self.create_binary_tree([2, 1, 3])
result = solution.isValidBST(root)
self.assertTrue(result)

def test_example_2(self):
# Example 2: Input: root = [5,1,4,null,null,3,6]
# Output: false
# Explanation: The root node's value is 5 but its right child's value is 4.
solution = Solution()
root = self.create_binary_tree([5, 1, 4, None, None, 3, 6])
result = solution.isValidBST(root)
self.assertFalse(result)