Skip to content

Commit 1107cdf

Browse files
committed
feat: add more question
1 parent bbf9560 commit 1107cdf

File tree

20 files changed

+576
-14
lines changed

20 files changed

+576
-14
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"problem_name": "binary_tree_right_side_view",
3+
"solution_class_name": "Solution",
4+
"problem_number": "199",
5+
"problem_title": "Binary Tree Right Side View",
6+
"difficulty": "Medium",
7+
"topics": "Tree, Depth-First Search, Breadth-First Search, Binary Tree",
8+
"tags": ["grind-75"],
9+
"readme_description": "Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return *the values of the nodes you can see ordered from top to bottom*.",
10+
"readme_examples": [
11+
{
12+
"content": "![Example 1](https://assets.leetcode.com/uploads/2024/11/24/tmpd5jn43fs-1.png)\n\n```\nInput: root = [1,2,3,null,5,null,4]\nOutput: [1,3,4]\n```"
13+
},
14+
{
15+
"content": "![Example 2](https://assets.leetcode.com/uploads/2024/11/24/tmpkpe40xeh-1.png)\n\n```\nInput: root = [1,2,3,4,null,null,null,5]\nOutput: [1,3,4,5]\n```"
16+
},
17+
{ "content": "```\nInput: root = [1,null,3]\nOutput: [1,3]\n```" },
18+
{ "content": "```\nInput: root = []\nOutput: []\n```" }
19+
],
20+
"readme_constraints": "- The number of nodes in the tree is in the range `[0, 100]`.\n- `-100 <= Node.val <= 100`",
21+
"readme_additional": "",
22+
"solution_imports": "from leetcode_py import TreeNode",
23+
"solution_methods": [
24+
{
25+
"name": "right_side_view",
26+
"parameters": "root: TreeNode[int] | None",
27+
"return_type": "list[int]",
28+
"dummy_return": "[]"
29+
}
30+
],
31+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom leetcode_py import TreeNode\nfrom .solution import Solution",
32+
"test_class_name": "BinaryTreeRightSideView",
33+
"test_helper_methods": [
34+
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }
35+
],
36+
"test_methods": [
37+
{
38+
"name": "test_right_side_view",
39+
"parametrize": "root_list, expected",
40+
"parametrize_typed": "root_list: list[int | None], expected: list[int]",
41+
"test_cases": "[([1, 2, 3, None, 5, None, 4], [1, 3, 4]), ([1, 2, 3, 4, None, None, None, 5], [1, 3, 4, 5]), ([1, None, 3], [1, 3]), ([], [])]",
42+
"body": "root = TreeNode.from_list(root_list)\nresult = self.solution.right_side_view(root)\nassert result == expected"
43+
}
44+
],
45+
"playground_imports": "from solution import Solution\nfrom leetcode_py import TreeNode",
46+
"playground_test_case": "# Example test case\nroot_list: list[int | None] = [1, 2, 3, None, 5, None, 4]\nexpected = [1, 3, 4]",
47+
"playground_execution": "root = TreeNode.from_list(root_list)\nresult = Solution().right_side_view(root)\nresult",
48+
"playground_assertion": "assert result == expected"
49+
}

.templates/leetcode/json/minimum_height_trees.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"problem_title": "Minimum Height Trees",
66
"difficulty": "Medium",
77
"topics": "Depth-First Search, Breadth-First Search, Graph, Topological Sort",
8-
"tags": [],
8+
"tags": ["grind-75"],
99
"readme_description": "A tree is an undirected graph in which any two vertices are connected by *exactly* one path. In other words, any connected graph without simple cycles is a tree.\n\nGiven a tree of `n` nodes labelled from `0` to `n - 1`, and an array of `n - 1` `edges` where `edges[i] = [ai, bi]` indicates that there is an undirected edge between the two nodes `ai` and `bi` in the tree, you can choose any node of the tree as the root. When you select a node `x` as the root, the result tree has height `h`. Among all possible rooted trees, those with minimum height (i.e. `min(h)`) are called **minimum height trees** (MHTs).\n\nReturn *a list of all **MHTs'** root labels*. You can return the answer in **any order**.\n\nThe **height** of a rooted tree is the number of edges on the longest downward path between the root and a leaf.",
1010
"readme_examples": [
1111
{
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"problem_name": "validate_binary_search_tree",
3+
"solution_class_name": "Solution",
4+
"problem_number": "98",
5+
"problem_title": "Validate Binary Search Tree",
6+
"difficulty": "Medium",
7+
"topics": "Tree, Depth-First Search, Binary Search Tree, Binary Tree",
8+
"tags": ["grind-75"],
9+
"readme_description": "Given the `root` of a binary tree, determine if it is a valid binary search tree (BST).\n\nA **valid BST** is defined as follows:\n\n- The left subtree of a node contains only nodes with keys **strictly less than** the node's key.\n- The right subtree of a node contains only nodes with keys **strictly greater than** the node's key.\n- Both the left and right subtrees must also be binary search trees.",
10+
"readme_examples": [
11+
{
12+
"content": "![Example 1](https://assets.leetcode.com/uploads/2020/12/01/tree1.jpg)\n\n```\nInput: root = [2,1,3]\nOutput: true\n```"
13+
},
14+
{
15+
"content": "![Example 2](https://assets.leetcode.com/uploads/2020/12/01/tree2.jpg)\n\n```\nInput: root = [5,1,4,null,null,3,6]\nOutput: false\n```\n**Explanation:** The root node's value is 5 but its right child's value is 4."
16+
}
17+
],
18+
"readme_constraints": "- The number of nodes in the tree is in the range `[1, 10^4]`.\n- `-2^31 <= Node.val <= 2^31 - 1`",
19+
"readme_additional": "",
20+
"solution_imports": "from leetcode_py import TreeNode",
21+
"solution_methods": [
22+
{
23+
"name": "is_valid_bst",
24+
"parameters": "root: TreeNode[int] | None",
25+
"return_type": "bool",
26+
"dummy_return": "False"
27+
}
28+
],
29+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom leetcode_py import TreeNode\nfrom .solution import Solution",
30+
"test_class_name": "ValidateBinarySearchTree",
31+
"test_helper_methods": [
32+
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }
33+
],
34+
"test_methods": [
35+
{
36+
"name": "test_is_valid_bst",
37+
"parametrize": "root_list, expected",
38+
"parametrize_typed": "root_list: list[int | None], expected: bool",
39+
"test_cases": "[([2, 1, 3], True), ([5, 1, 4, None, None, 3, 6], False), ([2, 1, 3], True), ([1], True), ([1, 1], False)]",
40+
"body": "root = TreeNode.from_list(root_list)\nresult = self.solution.is_valid_bst(root)\nassert result == expected"
41+
}
42+
],
43+
"playground_imports": "from solution import Solution\nfrom leetcode_py import TreeNode",
44+
"playground_test_case": "# Example test case\nroot_list: list[int | None] = [2, 1, 3]\nexpected = True",
45+
"playground_execution": "root = TreeNode.from_list(root_list)\nresult = Solution().is_valid_bst(root)\nresult",
46+
"playground_assertion": "assert result == expected"
47+
}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PYTHON_VERSION = 3.13
2-
PROBLEM ?= minimum_height_trees
2+
PROBLEM ?= binary_tree_right_side_view
33
FORCE ?= 0
44

55
sync_submodules:
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Binary Tree Right Side View
2+
3+
**Difficulty:** Medium
4+
**Topics:** Tree, Depth-First Search, Breadth-First Search, Binary Tree
5+
**Tags:** grind-75
6+
7+
**LeetCode:** [Problem 199](https://leetcode.com/problems/binary-tree-right-side-view/description/)
8+
9+
## Problem Description
10+
11+
Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return _the values of the nodes you can see ordered from top to bottom_.
12+
13+
## Examples
14+
15+
### Example 1:
16+
17+
![Example 1](https://assets.leetcode.com/uploads/2024/11/24/tmpd5jn43fs-1.png)
18+
19+
```
20+
Input: root = [1,2,3,null,5,null,4]
21+
Output: [1,3,4]
22+
```
23+
24+
### Example 2:
25+
26+
![Example 2](https://assets.leetcode.com/uploads/2024/11/24/tmpkpe40xeh-1.png)
27+
28+
```
29+
Input: root = [1,2,3,4,null,null,null,5]
30+
Output: [1,3,4,5]
31+
```
32+
33+
### Example 3:
34+
35+
```
36+
Input: root = [1,null,3]
37+
Output: [1,3]
38+
```
39+
40+
### Example 4:
41+
42+
```
43+
Input: root = []
44+
Output: []
45+
```
46+
47+
## Constraints
48+
49+
- The number of nodes in the tree is in the range `[0, 100]`.
50+
- `-100 <= Node.val <= 100`

leetcode/binary_tree_right_side_view/__init__.py

Whitespace-only changes.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "imports",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from solution import Solution\n",
11+
"\n",
12+
"from leetcode_py import TreeNode"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": null,
18+
"id": "setup",
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"# Example test case\n",
23+
"root_list: list[int | None] = [1, 2, 3, None, 5, None, 4]\n",
24+
"expected = [1, 3, 4]"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": null,
30+
"id": "execute",
31+
"metadata": {},
32+
"outputs": [],
33+
"source": [
34+
"root = TreeNode.from_list(root_list)\n",
35+
"result = Solution().right_side_view(root)\n",
36+
"result"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"id": "test",
43+
"metadata": {},
44+
"outputs": [],
45+
"source": [
46+
"assert result == expected"
47+
]
48+
}
49+
],
50+
"metadata": {
51+
"kernelspec": {
52+
"display_name": "leetcode-py-py3.13",
53+
"language": "python",
54+
"name": "python3"
55+
},
56+
"language_info": {
57+
"codemirror_mode": {
58+
"name": "ipython",
59+
"version": 3
60+
},
61+
"file_extension": ".py",
62+
"mimetype": "text/x-python",
63+
"name": "python",
64+
"nbconvert_exporter": "python3",
65+
"pygments_lexer": "ipython3",
66+
"version": "3.13.7"
67+
}
68+
},
69+
"nbformat": 4,
70+
"nbformat_minor": 5
71+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from collections import deque
2+
3+
from leetcode_py import TreeNode
4+
5+
6+
class Solution:
7+
# Time: O(n)
8+
# Space: O(h)
9+
def right_side_view(self, root: TreeNode[int] | None) -> list[int]:
10+
result: list[int] = []
11+
12+
def dfs(node: TreeNode[int] | None, level: int) -> None:
13+
if not node:
14+
return
15+
if level == len(result):
16+
result.append(node.val)
17+
dfs(node.right, level + 1)
18+
dfs(node.left, level + 1)
19+
20+
dfs(root, 0)
21+
return result
22+
23+
24+
class SolutionDFS:
25+
# Time: O(n)
26+
# Space: O(h)
27+
def right_side_view(self, root: TreeNode[int] | None) -> list[int]:
28+
if not root:
29+
return []
30+
31+
result: list[int] = []
32+
stack = [(root, 0)]
33+
34+
while stack:
35+
node, level = stack.pop()
36+
if level == len(result):
37+
result.append(node.val)
38+
if node.left:
39+
stack.append((node.left, level + 1))
40+
if node.right:
41+
stack.append((node.right, level + 1))
42+
43+
return result
44+
45+
46+
class SolutionBFS:
47+
# Time: O(n)
48+
# Space: O(w)
49+
def right_side_view(self, root: TreeNode[int] | None) -> list[int]:
50+
if not root:
51+
return []
52+
53+
result: list[int] = []
54+
queue = deque([root])
55+
56+
while queue:
57+
level_size = len(queue)
58+
for i in range(level_size):
59+
node = queue.popleft()
60+
if i == level_size - 1: # rightmost node
61+
result.append(node.val)
62+
if node.left:
63+
queue.append(node.left)
64+
if node.right:
65+
queue.append(node.right)
66+
67+
return result
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pytest
2+
3+
from leetcode_py import TreeNode
4+
from leetcode_py.test_utils import logged_test
5+
6+
from .solution import Solution, SolutionBFS, SolutionDFS
7+
8+
9+
class TestBinaryTreeRightSideView:
10+
@pytest.mark.parametrize("solution_class", [Solution, SolutionDFS, SolutionBFS])
11+
@pytest.mark.parametrize(
12+
"root_list, expected",
13+
[
14+
([1, 2, 3, None, 5, None, 4], [1, 3, 4]),
15+
([1, 2, 3, 4, None, None, None, 5], [1, 3, 4, 5]),
16+
([1, None, 3], [1, 3]),
17+
([], []),
18+
],
19+
)
20+
@logged_test
21+
def test_right_side_view(
22+
self,
23+
root_list: list[int | None],
24+
expected: list[int],
25+
solution_class: type[Solution | SolutionDFS | SolutionBFS],
26+
):
27+
solution = solution_class()
28+
root = TreeNode.from_list(root_list)
29+
result = solution.right_side_view(root)
30+
assert result == expected

leetcode/container_with_most_water/playground.ipynb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"metadata": {},
3030
"outputs": [],
3131
"source": [
32-
"result = Solution().max_area(height)\nresult"
32+
"result = Solution().max_area(height)\n",
33+
"result"
3334
]
3435
},
3536
{

0 commit comments

Comments
 (0)