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,17 @@
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
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,29 @@
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 lowestCommonAncestor(self, root:TreeNode, p, q):
"""
:type root: TreeNode
:type p: TreeNode
:type q: TreeNode
:rtype: TreeNode
"""

if not root:
return
elif root.val == p.val or root.val == q.val:
return root

l = self.lowestCommonAncestor(root.left,p,q)
r = self.lowestCommonAncestor(root.right,p,q)
if l and r:
return root
else:
return l or r
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TreeNode } from './TreeNode';

function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null {
if (root === null || root === p || root === q) {
return root;
}

const left = lowestCommonAncestor(root.left, p, q);
const right = lowestCommonAncestor(root.right, p, q);

if (left !== null && right !== null) {
return root;
}

return left !== null ? left : right;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import unittest
from typing import Optional, List
from src.my_project.interviews.top_150_questions_round_22\
.ex_78_lowest_common_ancestor_of_binary_tree import Solution, TreeNode


class LowestCommonAncestorBinaryTreeTestCase(unittest.TestCase):

def build_tree(self, values: List[Optional[int]]) -> Optional[TreeNode]:
"""Build a binary tree from level-order array representation."""
if not values or values[0] is None:
return None

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

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

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

# 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 find_node(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
"""Find a node with given value in the tree."""
if not root:
return None
if root.val == val:
return root
left = self.find_node(root.left, val)
if left:
return left
return self.find_node(root.right, val)

def test_example_1(self):
"""Test case: LCA of nodes 5 and 1 is 3."""
solution = Solution()
tree = self.build_tree([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4])
p = self.find_node(tree, 5)
q = self.find_node(tree, 1)
output = solution.lowestCommonAncestor(root=tree, p=p, q=q)
self.assertEqual(3, output.val)

def test_example_2(self):
"""Test case: LCA of nodes 5 and 4 is 5 (node can be ancestor of itself)."""
solution = Solution()
tree = self.build_tree([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4])
p = self.find_node(tree, 5)
q = self.find_node(tree, 4)
output = solution.lowestCommonAncestor(root=tree, p=p, q=q)
self.assertEqual(5, output.val)

def test_example_3(self):
"""Test case: LCA of nodes 1 and 2 is 1."""
solution = Solution()
tree = self.build_tree([1, 2])
p = self.find_node(tree, 1)
q = self.find_node(tree, 2)
output = solution.lowestCommonAncestor(root=tree, p=p, q=q)
self.assertEqual(1, output.val)