Skip to content

Commit bdb3cc3

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 98, 230 and 530
1 parent 3b711af commit bdb3cc3

7 files changed

+149
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
3737
- [71 Simplify Path](https://leetcode.com/problems/simplify-path/description/)
3838
- [80 Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/description/)
3939
- [88 Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/description/)
40+
- [98 Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/description/)
4041
- [100 Same Tree](https://leetcode.com/problems/same-tree/description/)
4142
- [101 Symmetric Tree](https://leetcode.com/problems/symmetric-tree/description/)
4243
- [102 Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/description/)
@@ -70,12 +71,14 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
7071
- [226 Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/description/)
7172
- [227 Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii/description/)
7273
- [228 Summary Ranges](https://leetcode.com/problems/summary-ranges/description/)
74+
- [230 Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/)
7375
- [236 Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/)
7476
- [242 Valid Anagram](https://leetcode.com/problems/valid-anagram/description/)
7577
- [290 Word Pattern](https://leetcode.com/problems/word-pattern/description/)
7678
- [383 Ransom Note](https://leetcode.com/problems/ransom-note/description/)
7779
- [392 Is Subsequence](https://leetcode.com/problems/is-subsequence/description/)
7880
- [452 Minimum Number of Arrows to Burst Balloons](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/)
81+
- [530 Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/)
7982
- [637 Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree/description/)
8083

8184
## Development 🔧
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 kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
10+
"""
11+
Given the root of a binary search tree, and an integer k, return the kth
12+
smallest value (1-indexed) of all the values of the nodes in the tree.
13+
"""
14+
res = 0
15+
16+
def dfs(root: Optional[TreeNode]):
17+
nonlocal res, k
18+
if root is None:
19+
return
20+
else:
21+
dfs(root.left)
22+
if k > 0:
23+
res = root.val
24+
k -= 1
25+
dfs(root.right)
26+
27+
dfs(root)
28+
return res
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 getMinimumDifference(self, root: Optional[TreeNode]) -> int:
10+
"""
11+
Given the root of a Binary Search Tree (BST), return the minimum absolute
12+
difference between the values of any two different nodes in the tree.
13+
"""
14+
prev, res = None, float("inf")
15+
16+
def dfs(root: Optional[TreeNode]):
17+
nonlocal prev, res
18+
if root is None:
19+
return
20+
dfs(root.left)
21+
if prev:
22+
res = min(res, abs(root.val - prev.val))
23+
prev = root
24+
dfs(root.right)
25+
26+
dfs(root)
27+
return res
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 isValidBST(self, root: Optional[TreeNode]) -> bool:
10+
"""
11+
Given the root of a binary tree, determine if it is a valid binary search
12+
tree (BST).
13+
14+
A valid BST is defined as follows:
15+
- The left subtree of a node contains only nodes with keys less than the node's
16+
key.
17+
- The right subtree of a node contains only nodes with keys greater than the
18+
node's key.
19+
- Both the left and right subtrees must also be binary search trees.
20+
"""
21+
22+
def dfs(root: Optional[TreeNode], left: int, right: int):
23+
if root is None:
24+
return True
25+
else:
26+
return (
27+
dfs(root.left, left, root.val)
28+
and dfs(root.right, root.val, right)
29+
and left < root.val
30+
and root.val < right
31+
)
32+
33+
return dfs(root, -float("inf"), +float("inf"))
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._230_kth_smallest_element_in_a_bst import Solution
6+
from awesome_python_leetcode.tree import TreeNode
7+
8+
9+
@pytest.mark.parametrize(
10+
argnames=["root", "k", "expected"],
11+
argvalues=[
12+
([3, 1, 4, None, 2], 1, 1),
13+
([5, 3, 6, 2, 4, None, None, 1], 3, 3),
14+
],
15+
)
16+
def test_func(root: List[int], k: int, expected: int):
17+
"""Tests the solution of a LeetCode problem."""
18+
root = TreeNode.build(root)
19+
kth_smallest = Solution().kthSmallest(root, k)
20+
assert kth_smallest == expected
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._530_minimum_absolute_difference_in_bst import Solution
6+
from awesome_python_leetcode.tree import TreeNode
7+
8+
9+
@pytest.mark.parametrize(
10+
argnames=["root", "expected"],
11+
argvalues=[([4, 2, 6, 1, 3], 1), ([1, 0, 48, None, None, 12, 49], 1)],
12+
)
13+
def test_func(root: List[int], expected: int):
14+
"""Tests the solution of a LeetCode problem."""
15+
root = TreeNode.build(root)
16+
minimum_difference = Solution().getMinimumDifference(root)
17+
assert minimum_difference == expected
Lines changed: 21 additions & 0 deletions
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._98_validate_binary_search_tree import Solution
6+
from awesome_python_leetcode.tree import TreeNode
7+
8+
9+
@pytest.mark.parametrize(
10+
argnames=["root", "expected"],
11+
argvalues=[
12+
([2, 1, 3], True),
13+
([5, 1, 4, None, None, 3, 6], False),
14+
([32, 26, 47, 19, None, None, 56, None, 27], False),
15+
],
16+
)
17+
def test_func(root: List[int], expected: bool):
18+
"""Tests the solution of a LeetCode problem."""
19+
root = TreeNode.build(root)
20+
is_valid_bst = Solution().isValidBST(root)
21+
assert is_valid_bst == expected

0 commit comments

Comments
 (0)