Skip to content

Commit 4534007

Browse files
committed
Run formatter
1 parent a5b39a6 commit 4534007

File tree

5 files changed

+43
-38
lines changed

5 files changed

+43
-38
lines changed

binary-tree-level-order-traversal/bhyun-kim.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55

66
from typing import List, Optional
77

8+
89
# Definition for a binary tree node.
910
class TreeNode:
1011
def __init__(self, val=0, left=None, right=None):
1112
self.val = val
1213
self.left = left
1314
self.right = right
1415

16+
1517
"""
1618
Solution
1719
Breadth First Search (BFS) using Queue
@@ -42,6 +44,8 @@ def __init__(self, val=0, left=None, right=None):
4244
- Thus, the space complexity is O(N)
4345
4446
"""
47+
48+
4549
class Solution:
4650
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
4751
if root is None:
@@ -50,18 +54,18 @@ def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
5054
queue = []
5155
queue.append(root)
5256

53-
while(len(queue) > 0):
57+
while len(queue) > 0:
5458
level_size = len(queue)
5559
level_output = []
56-
60+
5761
while level_size > 0:
5862
node = queue.pop(0)
5963
level_output.append(node.val)
6064

6165
if node.left is not None:
6266
queue.append(node.left)
6367
if node.right is not None:
64-
queue.append(node.right)
68+
queue.append(node.right)
6569

6670
level_size -= 1
6771

lowest-common-ancestor-of-a-binary-search-tree/bhyun-kim.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
44
"""
55

6+
67
# Definition for a binary tree node.
78
class TreeNode:
89
def __init__(self, x):
910
self.val = x
1011
self.left = None
1112
self.right = None
1213

14+
1315
"""
1416
Solution:
1517
- If both p and q are greater than the root, the lowest common ancestor is in the right subtree.
@@ -22,15 +24,15 @@ def __init__(self, x):
2224
- Maximum depth of the recursion is the height of the tree
2325
"""
2426

27+
2528
class Solution:
2629
def lowestCommonAncestor(
2730
self, root: "TreeNode", p: "TreeNode", q: "TreeNode"
2831
) -> "TreeNode":
29-
30-
if p.val > root.val and q.val > root.val:
32+
if p.val > root.val and q.val > root.val:
3133
return self.lowestCommonAncestor(root.right, p, q)
32-
33-
elif p.val < root.val and q.val < root.val:
34+
35+
elif p.val < root.val and q.val < root.val:
3436
return self.lowestCommonAncestor(root.left, p, q)
3537
else:
36-
return root
38+
return root

remove-nth-node-from-end-of-list/bhyun-kim.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
from typing import Optional
88

9+
910
class ListNode:
1011
def __init__(self, val=0, next=None):
1112
self.val = val
1213
self.next = next
1314

15+
1416
"""
1517
Solution 1:
1618
Two Pass Algorithm 1
@@ -24,9 +26,9 @@ def __init__(self, val=0, next=None):
2426
- The new nodes are created to build the new list
2527
"""
2628

29+
2730
class Solution1:
2831
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
29-
3032
vals = []
3133

3234
while head:
@@ -36,14 +38,13 @@ def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNod
3638
dummy_node = ListNode()
3739
tail = dummy_node
3840
idx_to_remove = len(vals) - n
39-
vals = vals[:idx_to_remove] + vals[idx_to_remove+1:]
41+
vals = vals[:idx_to_remove] + vals[idx_to_remove + 1 :]
4042

4143
for v in vals:
4244
tail.next = ListNode(val=v)
4345
tail = tail.next
44-
46+
4547
return dummy_node.next
46-
4748

4849

4950
"""
@@ -61,20 +62,20 @@ def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNod
6162
- No extra space is required
6263
"""
6364

65+
6466
class Solution2:
6567
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
66-
6768
dummy = ListNode()
6869
dummy.next = head
6970
first = dummy
7071
second = dummy
7172

72-
for _ in range(n+1):
73+
for _ in range(n + 1):
7374
first = first.next
7475

7576
while first:
7677
first = first.next
77-
second = second.next
78+
second = second.next
7879

7980
second.next = second.next.next
8081

reorder-list/bhyun-kim.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
from typing import Optional
77

8+
89
class ListNode:
910
def __init__(self, val=0, next=None):
1011
self.val = val
1112
self.next = next
1213

14+
1315
"""
1416
Solution:
1517
To reorder the linked list, we can follow these steps:
@@ -52,7 +54,7 @@ def reorderList(self, head: Optional[ListNode]) -> None:
5254
"""
5355
if not head or not head.next:
5456
return
55-
57+
5658
slow, fast = head, head
5759
while fast and fast.next:
5860
slow = slow.next
@@ -70,4 +72,4 @@ def reorderList(self, head: Optional[ListNode]) -> None:
7072
tmp1, tmp2 = first.next, second.next
7173
first.next = second
7274
second.next = tmp1
73-
first, second = tmp1, tmp2
75+
first, second = tmp1, tmp2

validate-binary-search-tree/bhyun-kim.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
"""
77
from typing import Optional
88

9+
910
class TreeNode:
1011
def __init__(self, val=0, left=None, right=None):
1112
self.val = val
1213
self.left = left
1314
self.right = right
1415

15-
16+
1617
"""
1718
Solution1:
1819
Recursion
@@ -34,44 +35,39 @@ def __init__(self, val=0, left=None, right=None):
3435
- The function stores the range of values for each node
3536
"""
3637

38+
3739
class Solution1:
3840
def isValidBST(self, root: Optional[TreeNode]) -> bool:
39-
maximum = float('inf')
40-
minimum = float('-inf')
41-
return self.isValidSubTree(
42-
root, maximum, minimum
43-
)
41+
maximum = float("inf")
42+
minimum = float("-inf")
43+
return self.isValidSubTree(root, maximum, minimum)
4444

4545
def isValidSubTree(self, root, maximum, minimum):
46-
if root.left is not None:
47-
if root.val <= root.left.val:
46+
if root.left is not None:
47+
if root.val <= root.left.val:
4848
return False
4949
if not minimum < root.left.val < maximum:
50-
return False
50+
return False
5151

52-
if root.right is not None :
53-
if root.val >= root.right.val:
52+
if root.right is not None:
53+
if root.val >= root.right.val:
5454
return False
5555
if not minimum < root.right.val < maximum:
56-
return False
56+
return False
5757

5858
if root.left is not None:
5959
l_max = min(maximum, root.val)
60-
is_left_valid = self.isValidSubTree(
61-
root.left, l_max, minimum
62-
)
60+
is_left_valid = self.isValidSubTree(root.left, l_max, minimum)
6361
else:
6462
is_left_valid = True
6563

6664
if root.right is not None:
6765
r_min = max(minimum, root.val)
68-
is_right_valid = self.isValidSubTree(
69-
root.right, maximum, r_min
70-
)
66+
is_right_valid = self.isValidSubTree(root.right, maximum, r_min)
7167
else:
7268
is_right_valid = True
73-
69+
7470
if is_left_valid and is_right_valid:
7571
return True
76-
else:
77-
return False
72+
else:
73+
return False

0 commit comments

Comments
 (0)