Skip to content

Commit 3b711af

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 102 and 103 - Fixed typing errors
1 parent 6073ef9 commit 3b711af

File tree

62 files changed

+180
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+180
-0
lines changed

Diff for: README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import List, Optional
2+
3+
from awesome_python_leetcode.tree import TreeNode
4+
5+
6+
class Solution:
7+
"""Base class for all LeetCode Problems."""
8+
9+
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
10+
"""
11+
Given the root of a binary tree, return the level order traversal of its nodes'
12+
values. (i.e., from left to right, level by level).
13+
"""
14+
if root is None:
15+
return []
16+
parents = [root]
17+
result = []
18+
while parents:
19+
# Build childs and level
20+
childs = []
21+
level = []
22+
for p in parents:
23+
if p.left:
24+
childs.append(p.left)
25+
if p.right:
26+
childs.append(p.right)
27+
level.append(p.val)
28+
parents = childs
29+
result.append(level)
30+
return result
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from typing import List, Optional
2+
3+
from awesome_python_leetcode.tree import TreeNode
4+
5+
6+
class Solution:
7+
"""Base class for all LeetCode Problems."""
8+
9+
def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
10+
"""
11+
Given the root of a binary tree, return the zigzag level order traversal of its
12+
nodes' values. (i.e., from left to right, then right to left for the next level
13+
and alternate between).
14+
"""
15+
if root is None:
16+
return []
17+
parents = [root]
18+
result = []
19+
even_level = True
20+
while parents:
21+
# Build childs and zig-zag levelorder
22+
childs = []
23+
levelorder = []
24+
for p in reversed(parents):
25+
if even_level:
26+
if p.left:
27+
childs.append(p.left)
28+
if p.right:
29+
childs.append(p.right)
30+
else:
31+
if p.right:
32+
childs.append(p.right)
33+
if p.left:
34+
childs.append(p.left)
35+
levelorder.append(p.val)
36+
parents = childs
37+
result.append(levelorder)
38+
even_level = not even_level
39+
return result

Diff for: awesome_python_leetcode/_136_single_number.py

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33

44
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
57
def singleNumber(self, nums: List[int]) -> int:
68
"""
79
Given a non-empty array of integers nums, every element appears twice except for

Diff for: awesome_python_leetcode/_137_single_number_II.py

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33

44
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
57
def singleNumber(self, nums: List[int]) -> int:
68
"""
79
Given an integer array nums where every element appears three times except for

Diff for: awesome_python_leetcode/_190_reverse_bits.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
24
def reverseBits(self, n: int) -> int:
35
"""Reverse bits of a given 32 bits unsigned integer."""
46
res = 0

Diff for: awesome_python_leetcode/_191_number_of_1_bits.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
24
def hammingWeight(self, n: int) -> int:
35
"""
46
Given a positive integer n, write a function that returns the number of set bits

Diff for: awesome_python_leetcode/_199_binary_tree_right_side_view.py

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55

66
class Solution:
7+
"""Base class for all LeetCode Problems."""
8+
79
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
810
"""
911
Given the root of a binary tree, imagine yourself standing on the right side of

Diff for: awesome_python_leetcode/_201_bitwise_and_of_numbers_range.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
24
def rangeBitwiseAnd(self, left: int, right: int) -> int:
35
"""
46
Given two integers left and right that represent the range [left, right],

Diff for: awesome_python_leetcode/_637_average_of_levels_in_binary_tree.py

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55

66
class Solution:
7+
"""Base class for all LeetCode Problems."""
8+
79
def averageOfLevels(self, root: Optional[TreeNode]) -> List[float]:
810
"""
911
Given the root of a binary tree, return the average value of the nodes on each

Diff for: awesome_python_leetcode/_67_add_binary.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
24
def addBinary(self, a: str, b: str) -> str:
35
"""Given two binary strings a and b, return their sum as a binary string."""
46
result, overhead = "", "0"

Diff for: tests/test_100_same_tree.py

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
],
1515
)
1616
def test_func(p: List[int], q: List[int], expected: bool):
17+
"""Tests the solution of a LeetCode problem."""
1718
p = TreeNode.build(p)
1819
q = TreeNode.build(q)
1920
is_same_tree = Solution().isSameTree(p, q)

Diff for: tests/test_101_symmetric_tree.py

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
],
1414
)
1515
def test_func(root: List[int], expected: bool):
16+
"""Tests the solution of a LeetCode problem."""
1617
root = TreeNode.build(root)
1718
is_symmetric = Solution().isSymmetric(root)
1819
assert is_symmetric == expected

Diff for: tests/test_102_binary_tree_level_order_traversal.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._102_binary_tree_level_order_traversal import Solution
6+
from awesome_python_leetcode.tree import TreeNode
7+
8+
9+
@pytest.mark.parametrize(
10+
argnames=["root", "expected"],
11+
argvalues=[
12+
([3, 9, 20, None, None, 15, 7], [[3], [9, 20], [15, 7]]),
13+
([1], [[1]]),
14+
([], []),
15+
],
16+
)
17+
def test_func(root: List[int], expected: List[List[int]]):
18+
"""Tests the solution of a LeetCode problem."""
19+
root = TreeNode.build(root)
20+
level_order = Solution().levelOrder(root)
21+
assert level_order == expected
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._103_binary_tree_zigzag_level_order_traversal import (
6+
Solution,
7+
)
8+
from awesome_python_leetcode.tree import TreeNode
9+
10+
11+
@pytest.mark.parametrize(
12+
argnames=["root", "expected"],
13+
argvalues=[
14+
([3, 9, 20, None, None, 15, 7], [[3], [20, 9], [15, 7]]),
15+
([1], [[1]]),
16+
([], []),
17+
],
18+
)
19+
def test_func(root: List[int], expected: List[List[int]]):
20+
"""Tests the solution of a LeetCode problem."""
21+
root = TreeNode.build(root)
22+
zig_zag_level_order = Solution().zigzagLevelOrder(root)
23+
assert zig_zag_level_order == expected

Diff for: tests/test_104_maximum_depth_of_binary_tree.py

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
],
1414
)
1515
def test_func(root: List[int], expected: int):
16+
"""Tests the solution of a LeetCode problem."""
1617
root = TreeNode.build(root)
1718
max_depth = Solution().maxDepth(root)
1819
assert max_depth == expected

Diff for: tests/test_105_construct_binary_tree_from_preorder_and_inorder_traversal.py

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
],
1818
)
1919
def test_func(preorder: List[int], inorder: List[int], expected: List[int]):
20+
"""Tests the solution of a LeetCode problem."""
2021
solution = TreeNode.build(expected)
2122
actual = Solution().buildTree(preorder, inorder)
2223
assert TreeNode.compare(actual, solution) is True

Diff for: tests/test_112_path_sum.py

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
],
1515
)
1616
def test_func(root: List[int], targetSum: int, expected: bool):
17+
"""Tests the solution of a LeetCode problem."""
1718
root = TreeNode.build(root)
1819
has_path_sum = Solution().hasPathSum(root, targetSum)
1920
assert has_path_sum == expected

Diff for: tests/test_114_flatten_binary_tree_to_linked_list.py

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
],
1818
)
1919
def test_func(root: List[int], expected: List[int]):
20+
"""Tests the solution of a LeetCode problem."""
2021
root = TreeNode.build(root)
2122
solution = TreeNode.build(expected)
2223
Solution().flatten(root)

Diff for: tests/test_117_populating_next_right_pointers_in_each_node_II.py

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
],
1818
)
1919
def test_func(root: List[int], expected: List[int]):
20+
"""Tests the solution of a LeetCode problem."""
2021
root = TreeNode.build(root)
2122
connected_root = Solution().connect(root)
2223
print(root)

Diff for: tests/test_121_best_time_to_buy_and_sell_stock.py

+1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
],
1414
)
1515
def test_func(prices: List[int], expected: int):
16+
"""Tests the solution of a LeetCode problem."""
1617
max_profit = Solution().maxProfit(prices)
1718
assert max_profit == expected

Diff for: tests/test_122_best_time_to_buy_and_sell_stock_II.py

+1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
],
1515
)
1616
def test_func(prices: List[int], expected: int):
17+
"""Tests the solution of a LeetCode problem."""
1718
max_profit = Solution().maxProfit(prices)
1819
assert max_profit == expected

Diff for: tests/test_124_binary_tree_maximum_path.py

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
],
1414
)
1515
def test_func(root: List[int], expected: int):
16+
"""Tests the solution of a LeetCode problem."""
1617
root = TreeNode.build(root)
1718
max_path_sum = Solution().maxPathSum(root)
1819
assert max_path_sum == expected

Diff for: tests/test_125_valid_palindrome.py

+1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
],
1313
)
1414
def test_func(s: str, expected: bool):
15+
"""Tests the solution of a LeetCode problem."""
1516
max_profit = Solution().isPalindrome(s)
1617
assert max_profit == expected

Diff for: tests/test_128_longest_consecutive_sequence.py

+1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
],
1414
)
1515
def test_func(nums: List[int], expected: int):
16+
"""Tests the solution of a LeetCode problem."""
1617
longest_consecutive = Solution().longestConsecutive(nums)
1718
assert longest_consecutive == expected

Diff for: tests/test_129_sum_root_to_leaf_numbers.py

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
],
1414
)
1515
def test_func(root: List[int], expected: int):
16+
"""Tests the solution of a LeetCode problem."""
1617
root = TreeNode.build(root)
1718
sum_root_to_leaf_numbers = Solution().sumNumbers(root)
1819
assert sum_root_to_leaf_numbers == expected

Diff for: tests/test_136_single_number.py

+1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
],
1414
)
1515
def test_func(nums: List[int], expected: int):
16+
"""Tests the solution of a LeetCode problem."""
1617
num = Solution().singleNumber(nums)
1718
assert num == expected

Diff for: tests/test_137_single_number.py

+1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
],
1313
)
1414
def test_func(nums: List[int], expected: str):
15+
"""Tests the solution of a LeetCode problem."""
1516
num = Solution().singleNumber(nums)
1617
assert num == expected

Diff for: tests/test_150_evaluate_reverse_polish_notation.py

+1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
],
1515
)
1616
def test_func(tokens: List[str], expected: int):
17+
"""Tests the solution of a LeetCode problem."""
1718
value = Solution().evalRPN(tokens)
1819
assert value == expected

Diff for: tests/test_155_min_stack.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
def test_func():
5+
"""Tests the solution of a LeetCode problem."""
56
min_stack = MinStack()
67
min_stack.push(-2)
78
min_stack.push(0)

Diff for: tests/test_169_majority_element.py

+1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
],
1414
)
1515
def test_func(nums: List[int], expected: int):
16+
"""Tests the solution of a LeetCode problem."""
1617
majority_element = Solution().majorityElement(nums)
1718
assert majority_element == expected

Diff for: tests/test_173_binary_search_tree_iterator.py

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66

77
def test_func():
8+
"""Tests the solution of a LeetCode problem."""
89
root = TreeNode.build([7, 3, 15, None, None, 9, 20])
910
iterator = BSTIterator(root)
1011
assert iterator.next() == 3

Diff for: tests/test_189_rotate_array.py

+1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
],
1414
)
1515
def test_func(nums: List[int], k: int, expected: int):
16+
"""Tests the solution of a LeetCode problem."""
1617
Solution().rotate(nums, k)
1718
assert nums == expected

Diff for: tests/test_190_reverse_bits.py

+1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
],
1212
)
1313
def test_func(n: int, expected: int):
14+
"""Tests the solution of a LeetCode problem."""
1415
n_reversed = Solution().reverseBits(n)
1516
assert n_reversed == expected

Diff for: tests/test_191_number_of_1_bits.py

+1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
],
1313
)
1414
def test_func(n: int, expected: int):
15+
"""Tests the solution of a LeetCode problem."""
1516
c = Solution().hammingWeight(n)
1617
assert c == expected

Diff for: tests/test_199_binary_tree_right_side_view.py

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
],
1717
)
1818
def test_func(root: List[int], expected: List[int]):
19+
"""Tests the solution of a LeetCode problem."""
1920
root = TreeNode.build(root)
2021
right_side_view = Solution().rightSideView(root)
2122
assert right_side_view == expected

Diff for: tests/test_201_bitwise_and_of_numbers_range.py

+1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
],
1313
)
1414
def test_func(left: int, right: int, expected: int):
15+
"""Tests the solution of a LeetCode problem."""
1516
range_bitwise_and = Solution().rangeBitwiseAnd(left, right)
1617
assert range_bitwise_and == expected

Diff for: tests/test_202_happy_number.py

+1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
],
1212
)
1313
def test_func(n: int, expected: bool):
14+
"""Tests the solution of a LeetCode problem."""
1415
is_happy = Solution().isHappy(n)
1516
assert is_happy == expected

Diff for: tests/test_205_isomorphic_strings.py

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
argvalues=[("egg", "add", True), ("foo", "bar", False), ("paper", "title", True)],
99
)
1010
def test_func(s: str, t: str, expected: bool):
11+
"""Tests the solution of a LeetCode problem."""
1112
is_isomorphic = Solution().isIsomorphic(s, t)
1213
assert is_isomorphic == expected

Diff for: tests/test_219_contains_duplicates_II.py

+1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
],
1515
)
1616
def test_func(nums: List[int], k: int, expected: bool):
17+
"""Tests the solution of a LeetCode problem."""
1718
contains_nearby_duplicate = Solution().containsNearbyDuplicate(nums, k)
1819
assert contains_nearby_duplicate == expected

Diff for: tests/test_222_count_complete_tree_nodes.py

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
],
1515
)
1616
def test_func(root: List[int], expected: int):
17+
"""Tests the solution of a LeetCode problem."""
1718
root = TreeNode.build(root)
1819
num_nodes = Solution().countNodes(root)
1920
assert num_nodes == expected

0 commit comments

Comments
 (0)