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,35 @@
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 rightSideView(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []

result = []
queue = deque([root])

while queue:
level_size = len(queue)

for i in range(level_size):
node = queue.popleft()

# Add the rightmost node of each level
if i == level_size - 1:
result.append(node.val)

# Add children to queue (left first, then right)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)

return result
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { TreeNode } from './TreeNode';

function rightSideView(root: TreeNode | null): number[] {
if (!root) {
return [];
}

const result: number[] = [];
const queue: TreeNode[] = [root];

while (queue.length > 0) {
const levelSize = queue.length;

for (let i = 0; i < levelSize; i++) {
const node = queue.shift()!;

// Add the rightmost node of each level
if (i === levelSize - 1) {
result.push(node.val);
}

// Add children to queue (left first, then right)
if (node.left) {
queue.push(node.left);
}
if (node.right) {
queue.push(node.right);
}
}
}

return result;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import unittest
from src.my_project.interviews.top_150_questions_round_22\
.ex_79_binary_tree_right_side_view import Solution, TreeNode

class BinaryTreeRightSideViewTestCase(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 = [1,2,3,null,5,null,4]
# Output: [1,3,4]
solution = Solution()
root = self.create_binary_tree([1, 2, 3, None, 5, None, 4])
result = solution.rightSideView(root)
self.assertEqual(result, [1, 3, 4])

def test_example_2(self):
# Example 2: Input: root = [1,2,3,4,null,null,null,5]
# Output: [1,3,4,5]
solution = Solution()
root = self.create_binary_tree([1, 2, 3, 4, None, None, None, 5])
result = solution.rightSideView(root)
self.assertEqual(result, [1, 3, 4, 5])

def test_example_3(self):
# Example 3: Input: root = [1,null,3]
# Output: [1,3]
solution = Solution()
root = self.create_binary_tree([1, None, 3])
result = solution.rightSideView(root)
self.assertEqual(result, [1, 3])

def test_example_4(self):
# Example 4: Input: root = []
# Output: []
solution = Solution()
root = self.create_binary_tree([])
result = solution.rightSideView(root)
self.assertEqual(result, [])