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,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 []
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,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 BSTIterator:

def __init__(self, root: Optional[TreeNode]):
self.stack = []
self._push_left(root)

def _push_left(self, node: Optional[TreeNode]) -> None:
"""Push all left children of a node onto the stack."""
while node:
self.stack.append(node)
node = node.left

def next(self) -> int:
# Pop the top node (next smallest element)
node = self.stack.pop()

# If it has a right child, push all left descendants of the right child
if node.right:
self._push_left(node.right)

return node.val

def hasNext(self) -> bool:
return len(self.stack) > 0

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

class BSTIterator {
private stack: TreeNode[];

constructor(root: TreeNode | null) {
this.stack = [];
this.pushLeft(root);
}

private pushLeft(node: TreeNode | null): void {
/**
* Push all left children of a node onto the stack.
*/
while (node !== null) {
this.stack.push(node);
node = node.left;
}
}

next(): number {
// Pop the top node (next smallest element)
const node = this.stack.pop()!;

// If it has a right child, push all left descendants of the right child
if (node.right !== null) {
this.pushLeft(node.right);
}

return node.val;
}

hasNext(): boolean {
return this.stack.length > 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import unittest
from typing import Optional, List
from src.my_project.interviews.top_150_questions_round_22\
.ex_76_binary_search_tree_iterator import BSTIterator, TreeNode


class BSTIteratorTestCase(unittest.TestCase):

def test_example_1(self):
"""
Input: ["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"]
[[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
Output: [null, 3, 7, true, 9, true, 15, true, 20, false]

Explanation:
BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
bSTIterator.next(); // return 3
bSTIterator.next(); // return 7
bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 9
bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 15
bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 20
bSTIterator.hasNext(); // return False
"""
# Build tree: [7, 3, 15, null, null, 9, 20]
# 7
# / \
# 3 15
# / \
# 9 20
root = TreeNode(7)
root.left = TreeNode(3)
root.right = TreeNode(15)
root.right.left = TreeNode(9)
root.right.right = TreeNode(20)

bst_iterator = BSTIterator(root)

self.assertEqual(bst_iterator.next(), 3)
self.assertEqual(bst_iterator.next(), 7)
self.assertTrue(bst_iterator.hasNext())
self.assertEqual(bst_iterator.next(), 9)
self.assertTrue(bst_iterator.hasNext())
self.assertEqual(bst_iterator.next(), 15)
self.assertTrue(bst_iterator.hasNext())
self.assertEqual(bst_iterator.next(), 20)
self.assertFalse(bst_iterator.hasNext())