Skip to content

Commit f1afa94

Browse files
committed
D. J.:
- Changed the workflow file for automated format checking - Changed the structure of the docstrings - Added the leetcode problem and solution for 100, 101, 104, 105, 106, 112, 114, 117, 124, 129, 173, 222, 226, and 236
1 parent 38f0073 commit f1afa94

File tree

64 files changed

+1037
-114
lines changed

Some content is hidden

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

64 files changed

+1037
-114
lines changed

.github/workflows/check-format.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ jobs:
2424
- name: Install dependencies
2525
run: |
2626
pip install -r requirements.txt
27-
pip install black
28-
- name: Check Format with Black
27+
pip install flake8
28+
- name: Check Format with flake8
2929
run: |
30-
black --check .
30+
# stop the build if there are Python syntax errors or undefined names
31+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
32+
# exit-zero treats all errors as warnings. Black has 88 max-line-length
33+
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=88 --statistics --ignore=E121,E123,E126,E203,E226,E24,E704,W503,W504
34+

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<img src="https://img.shields.io/badge/tests-passed-brightgreen">
1616
</a>
1717
<a>
18-
<img src="https://img.shields.io/badge/coverage-98%25-brightgreen">
18+
<img src="https://img.shields.io/badge/coverage-97%25-brightgreen">
1919
</a>
2020
</h1>
2121
</div>
@@ -36,20 +36,34 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
3636
- [71 Simplify Path](https://leetcode.com/problems/simplify-path/description/)
3737
- [80 Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/description/)
3838
- [88 Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/description/)
39+
- [100 Same Tree](https://leetcode.com/problems/same-tree/description/)
40+
- [101 Symmetric Tree](https://leetcode.com/problems/symmetric-tree/description/)
41+
- [104 Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/description/)
42+
- [105 Construct Binary Tree from Preorder and Inorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/)
43+
- [106 Construct Binary Tree from Inorder and Postorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/)
44+
- [112 Path Sum](https://leetcode.com/problems/path-sum/description/)
45+
- [114 Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/description/)
46+
- [117 Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/description/)
3947
- [121 Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/)
4048
- [122 Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/)
49+
- [124 Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/description/)
4150
- [125 Valid Palindrome](https://leetcode.com/problems/valid-palindrome/description/)
4251
- [128 Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/description/)
52+
- [129 Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/description/)
4353
- [150 Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/description/)
4454
- [155 Min Stack](https://leetcode.com/problems/min-stack/description/)
4555
- [169 Majority Element](https://leetcode.com/problems/majority-element/description/)
56+
- [173 Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/description/)
4657
- [189 Rotate Array](https://leetcode.com/problems/rotate-array/description/)
4758
- [202 Happy Number](https://leetcode.com/problems/happy-number/description/)
4859
- [205 Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/description/)
4960
- [219 Contains Duplicates II](https://leetcode.com/problems/contains-duplicate-ii/description/)
61+
- [222 Count Complete Tree Nodes](https://leetcode.com/problems/count-complete-tree-nodes/description/)
5062
- [224 Basic Calculator](https://leetcode.com/problems/basic-calculator/description/)
63+
- [226 Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/description/)
5164
- [227 Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii/description/)
5265
- [228 Summary Ranges](https://leetcode.com/problems/summary-ranges/description/)
66+
- [236 Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/)
5367
- [242 Valid Anagram](https://leetcode.com/problems/valid-anagram/description/)
5468
- [290 Word Pattern](https://leetcode.com/problems/word-pattern/description/)
5569
- [383 Ransom Note](https://leetcode.com/problems/ransom-note/description/)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import 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 isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
10+
"""
11+
Given the roots of two binary trees p and q, write a function to check if they
12+
are the same or not.
13+
14+
Two binary trees are considered the same if they are structurally identical, and
15+
the nodes have the same value.
16+
"""
17+
if p is None and q is None:
18+
return True
19+
elif p is None:
20+
return False
21+
elif q is None:
22+
return False
23+
else:
24+
return (
25+
p.val == q.val
26+
and self.isSameTree(p.left, q.left)
27+
and self.isSameTree(p.right, q.right)
28+
)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import Optional
2+
3+
4+
from awesome_python_leetcode.tree import TreeNode
5+
6+
7+
class Solution:
8+
"""Base class for all LeetCode Problems."""
9+
10+
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
11+
"""
12+
Given the root of a binary tree, check whether it is a mirror of itself
13+
(i.e., symmetric around its center).
14+
"""
15+
16+
def _is_symmetric(left: Optional[TreeNode], right: Optional[TreeNode]) -> bool:
17+
if left is None and right is None:
18+
return True
19+
elif left is None:
20+
return False
21+
elif right is None:
22+
return False
23+
else:
24+
return (
25+
left.val == right.val
26+
and _is_symmetric(left.left, right.right)
27+
and _is_symmetric(left.right, right.left)
28+
)
29+
30+
if root is None:
31+
return True
32+
return _is_symmetric(root.left, root.right)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import Optional
2+
3+
4+
from awesome_python_leetcode.tree import TreeNode
5+
6+
7+
class Solution:
8+
"""Base class for all LeetCode Problems."""
9+
10+
def maxDepth(self, root: Optional[TreeNode]) -> int:
11+
"""
12+
Given the root of a binary tree, return its maximum depth.
13+
14+
A binary tree's maximum depth is the number of nodes along the longest path
15+
from the root node down to the farthest leaf node.
16+
"""
17+
if root is None:
18+
return 0
19+
else:
20+
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from typing import List, Optional
2+
3+
4+
from awesome_python_leetcode.tree import TreeNode
5+
6+
7+
class Solution:
8+
"""Base class for all LeetCode Problems."""
9+
10+
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
11+
"""
12+
Given two integer arrays preorder and inorder where preorder is the preorder
13+
traversal of a binary tree and inorder is the inorder traversal of the same
14+
tree, construct and return the binary tree.
15+
"""
16+
if not preorder or not inorder:
17+
return None
18+
root = preorder[0]
19+
mid = inorder.index(root)
20+
return TreeNode(
21+
val=root,
22+
left=self.buildTree(preorder[1 : mid + 1], inorder[:mid]),
23+
right=self.buildTree(preorder[mid + 1 :], inorder[mid + 1 :]),
24+
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing import List, Optional
2+
3+
4+
from awesome_python_leetcode.tree import TreeNode
5+
6+
7+
class Solution:
8+
"""Base class for all LeetCode Problems."""
9+
10+
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
11+
"""
12+
Given two integer arrays inorder and postorder where inorder is the inorder
13+
traversal of a binary tree and postorder is the postorder traversal of the same
14+
tree, construct and return the binary tree.
15+
"""
16+
if not inorder or not postorder:
17+
return None
18+
else:
19+
root = postorder.pop(-1)
20+
mid = inorder.index(root)
21+
return TreeNode(
22+
val=root,
23+
right=self.buildTree(inorder[mid + 1 :], postorder),
24+
left=self.buildTree(inorder[:mid], postorder),
25+
)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import Optional
2+
3+
4+
from awesome_python_leetcode.tree import TreeNode
5+
6+
7+
class Solution:
8+
"""Base class for all LeetCode Problems."""
9+
10+
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
11+
"""
12+
Given the root of a binary tree and an integer targetSum, return true if the
13+
tree has a root-to-leaf path such that adding up all the values along the path
14+
equals targetSum.
15+
16+
A leaf is a node with no children.
17+
"""
18+
if root is None:
19+
return False
20+
elif root.left is None and root.right is None:
21+
return targetSum - root.val == 0
22+
elif root.left is None:
23+
return self.hasPathSum(root.right, targetSum - root.val)
24+
elif root.right is None:
25+
return self.hasPathSum(root.left, targetSum - root.val)
26+
else:
27+
return self.hasPathSum(root.left, targetSum - root.val) or self.hasPathSum(
28+
root.right, targetSum - root.val
29+
)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import Optional
2+
3+
4+
from awesome_python_leetcode.tree import TreeNode
5+
6+
7+
class Solution:
8+
"""Base class for all LeetCode Problems."""
9+
10+
def flatten(self, root: Optional[TreeNode]):
11+
"""
12+
Given the root of a binary tree, flatten the tree into a "linked list":
13+
- The "linked list" should use the same TreeNode class where the right child
14+
pointer points to the next node in the list and the left child pointer is always
15+
null.
16+
- The "linked list" should be in the same order as a pre-order traversal of the
17+
binary tree.
18+
- Do not return anything, modify root in-place instead.
19+
"""
20+
21+
def dfs(root: Optional[TreeNode]) -> Optional[TreeNode]:
22+
if not root:
23+
return None
24+
25+
left = dfs(root.left)
26+
right = dfs(root.right)
27+
28+
if root.left:
29+
left.right = root.right
30+
root.right = root.left
31+
root.left = None
32+
33+
last = right or left or root
34+
return last
35+
36+
dfs(root)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from awesome_python_leetcode.tree import TreeNode
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def connect(self, root: TreeNode) -> TreeNode:
8+
"""
9+
Populate each next pointer to point to its next right node. If there is no next
10+
right node, the next pointer should be set to NULL.
11+
12+
Initially, all next pointers are set to NULL.
13+
"""
14+
if root is None:
15+
return None
16+
17+
parents = [root]
18+
while parents:
19+
# Connect nodes together
20+
for i in range(len(parents) - 1):
21+
parents[i].next = parents[i + 1]
22+
23+
# Build next layer
24+
children = []
25+
for parent in parents:
26+
if parent.left is not None and parent.right is not None:
27+
children.append(parent.left)
28+
children.append(parent.right)
29+
elif parent.left is not None:
30+
children.append(parent.left)
31+
elif parent.right is not None:
32+
children.append(parent.right)
33+
34+
# Update layer
35+
parents = children
36+
return root

0 commit comments

Comments
 (0)