Skip to content

Commit a64905d

Browse files
authored
Merge pull request #1550 from ivanpenaloza/february16
adding algo
2 parents a2d0302 + 426aec1 commit a64905d

5 files changed

Lines changed: 155 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def twoSum(self, nums: List[int], target: int) -> List[int]:
6+
7+
answer = dict()
8+
9+
for k, v in enumerate(nums):
10+
11+
if v in answer:
12+
return [answer[v], k]
13+
else:
14+
answer[target - v] = k
15+
16+
return []
17+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
import re
4+
5+
class Solution:
6+
def isPalindrome(self, s: str) -> bool:
7+
8+
# To lowercase
9+
s = s.lower()
10+
11+
# Remove non-alphanumeric
12+
s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s)
13+
14+
# Determine if s is palindrome or not
15+
len_s = len(s)
16+
17+
for i in range(len_s//2):
18+
19+
if s[i] != s[len_s - 1 - i]:
20+
return False
21+
22+
return True
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class TreeNode:
5+
def __init__(self, val=0, left=None, right=None):
6+
self.val = val
7+
self.left = left
8+
self.right = right
9+
10+
class Solution:
11+
def lowestCommonAncestor(self, root:TreeNode, p, q):
12+
"""
13+
:type root: TreeNode
14+
:type p: TreeNode
15+
:type q: TreeNode
16+
:rtype: TreeNode
17+
"""
18+
19+
if not root:
20+
return
21+
elif root.val == p.val or root.val == q.val:
22+
return root
23+
24+
l = self.lowestCommonAncestor(root.left,p,q)
25+
r = self.lowestCommonAncestor(root.right,p,q)
26+
if l and r:
27+
return root
28+
else:
29+
return l or r
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { TreeNode } from './TreeNode';
2+
3+
function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null {
4+
if (root === null || root === p || root === q) {
5+
return root;
6+
}
7+
8+
const left = lowestCommonAncestor(root.left, p, q);
9+
const right = lowestCommonAncestor(root.right, p, q);
10+
11+
if (left !== null && right !== null) {
12+
return root;
13+
}
14+
15+
return left !== null ? left : right;
16+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import unittest
2+
from typing import Optional, List
3+
from src.my_project.interviews.top_150_questions_round_22\
4+
.ex_78_lowest_common_ancestor_of_binary_tree import Solution, TreeNode
5+
6+
7+
class LowestCommonAncestorBinaryTreeTestCase(unittest.TestCase):
8+
9+
def build_tree(self, values: List[Optional[int]]) -> Optional[TreeNode]:
10+
"""Build a binary tree from level-order array representation."""
11+
if not values or values[0] is None:
12+
return None
13+
14+
root = TreeNode(values[0])
15+
queue = [root]
16+
i = 1
17+
18+
while queue and i < len(values):
19+
node = queue.pop(0)
20+
21+
# Left child
22+
if i < len(values) and values[i] is not None:
23+
node.left = TreeNode(values[i])
24+
queue.append(node.left)
25+
i += 1
26+
27+
# Right child
28+
if i < len(values) and values[i] is not None:
29+
node.right = TreeNode(values[i])
30+
queue.append(node.right)
31+
i += 1
32+
33+
return root
34+
35+
def find_node(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
36+
"""Find a node with given value in the tree."""
37+
if not root:
38+
return None
39+
if root.val == val:
40+
return root
41+
left = self.find_node(root.left, val)
42+
if left:
43+
return left
44+
return self.find_node(root.right, val)
45+
46+
def test_example_1(self):
47+
"""Test case: LCA of nodes 5 and 1 is 3."""
48+
solution = Solution()
49+
tree = self.build_tree([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4])
50+
p = self.find_node(tree, 5)
51+
q = self.find_node(tree, 1)
52+
output = solution.lowestCommonAncestor(root=tree, p=p, q=q)
53+
self.assertEqual(3, output.val)
54+
55+
def test_example_2(self):
56+
"""Test case: LCA of nodes 5 and 4 is 5 (node can be ancestor of itself)."""
57+
solution = Solution()
58+
tree = self.build_tree([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4])
59+
p = self.find_node(tree, 5)
60+
q = self.find_node(tree, 4)
61+
output = solution.lowestCommonAncestor(root=tree, p=p, q=q)
62+
self.assertEqual(5, output.val)
63+
64+
def test_example_3(self):
65+
"""Test case: LCA of nodes 1 and 2 is 1."""
66+
solution = Solution()
67+
tree = self.build_tree([1, 2])
68+
p = self.find_node(tree, 1)
69+
q = self.find_node(tree, 2)
70+
output = solution.lowestCommonAncestor(root=tree, p=p, q=q)
71+
self.assertEqual(1, output.val)

0 commit comments

Comments
 (0)