Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit39499ca

Browse files
authored
Added tasks 226-295
1 parent4b7c413 commit39499ca

File tree

21 files changed

+779
-0
lines changed

21 files changed

+779
-0
lines changed

‎README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,14 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
5555
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
5656
|-|-|-|-|-|-
5757
| 0121 |[Best Time to Buy and Sell Stock](src/main/rust/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.rs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 3 | 98.62
58+
| 0283 |[Move Zeroes](src/main/rust/g0201_0300/s0283_move_zeroes/Solution.rs)| Easy | Top_100_Liked_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
5859
| 0001 |[Two Sum](src/main/rust/g0001_0100/s0001_two_sum/Solution.rs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
5960
| 0189 |[Rotate Array](src/main/rust/g0101_0200/s0189_rotate_array/Solution.rs)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 3 | 96.69
6061
| 0055 |[Jump Game](src/main/rust/g0001_0100/s0055_jump_game/Solution.rs)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
6162
| 0075 |[Sort Colors](src/main/rust/g0001_0100/s0075_sort_colors/Solution.rs)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
63+
| 0238 |[Product of Array Except Self](src/main/rust/g0201_0300/s0238_product_of_array_except_self/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Prefix_Sum, Big_O_Time_O(n^2)_Space_O(n) | 8 | 87.13
6264
| 0041 |[First Missing Positive](src/main/rust/g0001_0100/s0041_first_missing_positive/Solution.rs)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 3 | 97.44
65+
| 0239 |[Sliding Window Maximum](src/main/rust/g0201_0300/s0239_sliding_window_maximum/Solution.rs)| Hard | Top_100_Liked_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue, Big_O_Time_O(n\*k)_Space_O(n+k) | 43 | 84.62
6366

6467
####Udemy Two Pointers
6568

@@ -97,6 +100,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
97100
| 0024 |[Swap Nodes in Pairs](src/main/rust/g0001_0100/s0024_swap_nodes_in_pairs/Solution.rs)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
98101
| 0206 |[Reverse Linked List](src/main/rust/g0201_0300/s0206_reverse_linked_list/Solution.rs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
99102
| 0021 |[Merge Two Sorted Lists](src/main/rust/g0001_0100/s0021_merge_two_sorted_lists/Solution.rs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00
103+
| 0234 |[Palindrome Linked List](src/main/rust/g0201_0300/s0234_palindrome_linked_list/Solution.rs)| Easy | Top_100_Liked_Questions, Two_Pointers, Stack, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 43 | 85.29
100104
| 0025 |[Reverse Nodes in k-Group](src/main/rust/g0001_0100/s0025_reverse_nodes_in_k_group/Solution.rs)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(k) | 0 | 100.00
101105
| 0146 |[LRU Cache](src/main/rust/g0101_0200/s0146_lru_cache/LRUCache.rs)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Design, Linked_List, Doubly_Linked_List, Big_O_Time_O(1)_Space_O(capacity) | 90 | 75.18
102106

@@ -106,9 +110,11 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
106110
|-|-|-|-|-|-
107111
| 0094 |[Binary Tree Inorder Traversal](src/main/rust/g0001_0100/s0094_binary_tree_inorder_traversal/Solution.rs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
108112
| 0102 |[Binary Tree Level Order Traversal](src/main/rust/g0101_0200/s0102_binary_tree_level_order_traversal/Solution.rs)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 1 | 80.61
113+
| 0226 |[Invert Binary Tree](src/main/rust/g0201_0300/s0226_invert_binary_tree/Solution.rs)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
109114
| 0104 |[Maximum Depth of Binary Tree](src/main/rust/g0101_0200/s0104_maximum_depth_of_binary_tree/Solution.rs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 1 | 80.46
110115
| 0124 |[Binary Tree Maximum Path Sum](src/main/rust/g0101_0200/s0124_binary_tree_maximum_path_sum/Solution.rs)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 0 | 100.00
111116
| 0098 |[Validate Binary Search Tree](src/main/rust/g0001_0100/s0098_validate_binary_search_tree/Solution.rs)| Medium | String, Dynamic_Programming | 1 | 77.46
117+
| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/rust/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/Solution.rs)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
112118

113119
####Udemy Trie and Heap
114120

@@ -227,6 +233,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
227233

228234
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
229235
|-|-|-|-|-|-
236+
| 0226 |[Invert Binary Tree](src/main/rust/g0201_0300/s0226_invert_binary_tree/Solution.rs)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
230237

231238
####Day 13 Tree
232239

@@ -266,11 +273,13 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
266273

267274
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
268275
|-|-|-|-|-|-
276+
| 0240 |[Search a 2D Matrix II](src/main/rust/g0201_0300/s0240_search_a_2d_matrix_ii/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer, Big_O_Time_O(n+m)_Space_O(1) | 0 | 100.00
269277

270278
####Day 5 Array
271279

272280
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
273281
|-|-|-|-|-|-
282+
| 0238 |[Product of Array Except Self](src/main/rust/g0201_0300/s0238_product_of_array_except_self/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Prefix_Sum, Big_O_Time_O(n^2)_Space_O(n) | 8 | 87.13
274283

275284
####Day 6 String
276285

@@ -338,11 +347,13 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
338347

339348
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
340349
|-|-|-|-|-|-
350+
| 0230 |[Kth Smallest Element in a BST](src/main/rust/g0201_0300/s0230_kth_smallest_element_in_a_bst/Solution.rs)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
341351

342352
####Day 18 Tree
343353

344354
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
345355
|-|-|-|-|-|-
356+
| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/rust/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/Solution.rs)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
346357

347358
####Day 19 Graph
348359

@@ -378,6 +389,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
378389

379390
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
380391
|-|-|-|-|-|-
392+
| 0283 |[Move Zeroes](src/main/rust/g0201_0300/s0283_move_zeroes/Solution.rs)| Easy | Top_100_Liked_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
381393

382394
####Day 4 Two Pointers
383395

@@ -660,6 +672,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
660672

661673
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
662674
|-|-|-|-|-|-
675+
| 0287 |[Find the Duplicate Number](src/main/rust/g0201_0300/s0287_find_the_duplicate_number/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Two_Pointers, Bit_Manipulation, Big_O_Time_O(n)_Space_O(n) | 4 | 98.95
663676

664677
####Day 6
665678

@@ -675,6 +688,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
675688

676689
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
677690
|-|-|-|-|-|-
691+
| 0240 |[Search a 2D Matrix II](src/main/rust/g0201_0300/s0240_search_a_2d_matrix_ii/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer, Big_O_Time_O(n+m)_Space_O(1) | 0 | 100.00
678692

679693
####Day 9
680694

@@ -889,6 +903,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
889903

890904
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
891905
|-|-|-|-|-|-
906+
| 0283 |[Move Zeroes](src/main/rust/g0201_0300/s0283_move_zeroes/Solution.rs)| Easy | Top_100_Liked_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
892907

893908
####Day 7 Array
894909

@@ -1255,6 +1270,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
12551270
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
12561271
|-|-|-|-|-|-
12571272
| 0019 |[Remove Nth Node From End of List](src/main/rust/g0001_0100/s0019_remove_nth_node_from_end_of_list/Solution.rs)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Linked_List, Big_O_Time_O(L)_Space_O(L) | 0 | 100.00
1273+
| 0234 |[Palindrome Linked List](src/main/rust/g0201_0300/s0234_palindrome_linked_list/Solution.rs)| Easy | Top_100_Liked_Questions, Two_Pointers, Stack, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 43 | 85.29
12581274

12591275
####Day 4 Linked List
12601276

@@ -1271,6 +1287,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
12711287

12721288
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
12731289
|-|-|-|-|-|-
1290+
| 0226 |[Invert Binary Tree](src/main/rust/g0201_0300/s0226_invert_binary_tree/Solution.rs)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
12741291

12751292
####Day 7 Tree
12761293

@@ -1288,6 +1305,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
12881305

12891306
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
12901307
|-|-|-|-|-|-
1308+
| 0230 |[Kth Smallest Element in a BST](src/main/rust/g0201_0300/s0230_kth_smallest_element_in_a_bst/Solution.rs)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
12911309

12921310
####Day 10 Graph/BFS/DFS
12931311

@@ -1358,6 +1376,16 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
13581376

13591377
| # | Title | Difficulty | Tag | Time, ms | Time, %
13601378
|------|----------------|-------------|-------------|----------|---------
1379+
| 0295 |[Find Median from Data Stream](src/main/rust/g0201_0300/s0295_find_median_from_data_stream/MedianFinder.rs)| Hard | Top_100_Liked_Questions, Sorting, Two_Pointers, Design, Heap_Priority_Queue, Data_Stream, Big_O_Time_O(n\*log_n)_Space_O(n) | 58 | 99.02
1380+
| 0287 |[Find the Duplicate Number](src/main/rust/g0201_0300/s0287_find_the_duplicate_number/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Two_Pointers, Bit_Manipulation, Binary_Search_II_Day_5, Big_O_Time_O(n)_Space_O(n) | 4 | 98.95
1381+
| 0283 |[Move Zeroes](src/main/rust/g0201_0300/s0283_move_zeroes/Solution.rs)| Easy | Top_100_Liked_Questions, Array, Two_Pointers, Algorithm_I_Day_3_Two_Pointers, Programming_Skills_I_Day_6_Array, Udemy_Arrays, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
1382+
| 0240 |[Search a 2D Matrix II](src/main/rust/g0201_0300/s0240_search_a_2d_matrix_ii/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer, Data_Structure_II_Day_4_Array, Binary_Search_II_Day_8, Big_O_Time_O(n+m)_Space_O(1) | 0 | 100.00
1383+
| 0239 |[Sliding Window Maximum](src/main/rust/g0201_0300/s0239_sliding_window_maximum/Solution.rs)| Hard | Top_100_Liked_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue, Udemy_Arrays, Big_O_Time_O(n\*k)_Space_O(n+k) | 43 | 84.62
1384+
| 0238 |[Product of Array Except Self](src/main/rust/g0201_0300/s0238_product_of_array_except_self/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Prefix_Sum, Data_Structure_II_Day_5_Array, Udemy_Arrays, Big_O_Time_O(n^2)_Space_O(n) | 8 | 87.13
1385+
| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/rust/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/Solution.rs)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Data_Structure_II_Day_18_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
1386+
| 0234 |[Palindrome Linked List](src/main/rust/g0201_0300/s0234_palindrome_linked_list/Solution.rs)| Easy | Top_100_Liked_Questions, Two_Pointers, Stack, Linked_List, Recursion, Level_2_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(n)_Space_O(1) | 43 | 85.29
1387+
| 0230 |[Kth Smallest Element in a BST](src/main/rust/g0201_0300/s0230_kth_smallest_element_in_a_bst/Solution.rs)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_II_Day_17_Tree, Level_2_Day_9_Binary_Search_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
1388+
| 0226 |[Invert Binary Tree](src/main/rust/g0201_0300/s0226_invert_binary_tree/Solution.rs)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_12_Tree, Level_2_Day_6_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
13611389
| 0221 |[Maximal Square](src/main/rust/g0201_0300/s0221_maximal_square/Solution.rs)| Medium | Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_16, Big_O_Time_O(m\*n)_Space_O(m\*n) | 16 | 88.89
13621390
| 0215 |[Kth Largest Element in an Array](src/main/rust/g0201_0300/s0215_kth_largest_element_in_an_array/Solution.rs)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Quickselect, Data_Structure_II_Day_20_Heap_Priority_Queue, Big_O_Time_O(n\*log(n))_Space_O(log(n)) | 10 | 90.24
13631391
| 0208 |[Implement Trie (Prefix Tree)](src/main/rust/g0201_0300/s0208_implement_trie_prefix_tree/Trie.rs)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Design, Trie, Level_2_Day_16_Design, Udemy_Trie_and_Heap, Big_O_Time_O(word.length())_or_O(prefix.length())_Space_O(N) | 13 | 90.59
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// #Easy #Top_100_Liked_Questions #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
2+
// #Data_Structure_I_Day_12_Tree #Level_2_Day_6_Tree #Udemy_Tree_Stack_Queue
3+
// #Big_O_Time_O(n)_Space_O(n) #2024_09_11_Time_0_ms_(100.00%)_Space_2_MB_(77.66%)
4+
5+
// Definition for a binary tree node.
6+
// #[derive(Debug, PartialEq, Eq)]
7+
// pub struct TreeNode {
8+
// pub val: i32,
9+
// pub left: Option<Rc<RefCell<TreeNode>>>,
10+
// pub right: Option<Rc<RefCell<TreeNode>>>,
11+
// }
12+
//
13+
// impl TreeNode {
14+
// #[inline]
15+
// pub fn new(val: i32) -> Self {
16+
// TreeNode {
17+
// val,
18+
// left: None,
19+
// right: None
20+
// }
21+
// }
22+
// }
23+
use std::rc::Rc;
24+
use std::cell::RefCell;
25+
implSolution{
26+
pubfninvert_tree(root:Option<Rc<RefCell<TreeNode>>>) ->Option<Rc<RefCell<TreeNode>>>{
27+
root.map(|node|{
28+
let node = node.borrow();
29+
letmut new_node =TreeNode::new(node.val);
30+
new_node.left =Self::invert_tree(node.right.clone());
31+
new_node.right =Self::invert_tree(node.left.clone());
32+
Rc::new(RefCell::new(new_node))
33+
})
34+
35+
}
36+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
226\. Invert Binary Tree
2+
3+
Easy
4+
5+
Given the`root` of a binary tree, invert the tree, and return_its root_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg)
10+
11+
**Input:** root =[4,2,7,1,3,6,9]
12+
13+
**Output:**[4,7,2,9,6,3,1]
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/03/14/invert2-tree.jpg)
18+
19+
**Input:** root =[2,1,3]
20+
21+
**Output:**[2,3,1]
22+
23+
**Example 3:**
24+
25+
**Input:** root =[]
26+
27+
**Output:**[]
28+
29+
**Constraints:**
30+
31+
* The number of nodes in the tree is in the range`[0, 100]`.
32+
*`-100 <= Node.val <= 100`
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// #Medium #Top_100_Liked_Questions #Depth_First_Search #Tree #Binary_Tree #Binary_Search_Tree
2+
// #Data_Structure_II_Day_17_Tree #Level_2_Day_9_Binary_Search_Tree #Big_O_Time_O(n)_Space_O(n)
3+
// #2024_09_11_Time_0_ms_(100.00%)_Space_3.1_MB_(15.07%)
4+
5+
// Definition for a binary tree node.
6+
// #[derive(Debug, PartialEq, Eq)]
7+
// pub struct TreeNode {
8+
// pub val: i32,
9+
// pub left: Option<Rc<RefCell<TreeNode>>>,
10+
// pub right: Option<Rc<RefCell<TreeNode>>>,
11+
// }
12+
//
13+
// impl TreeNode {
14+
// #[inline]
15+
// pub fn new(val: i32) -> Self {
16+
// TreeNode {
17+
// val,
18+
// left: None,
19+
// right: None
20+
// }
21+
// }
22+
// }
23+
use std::rc::Rc;
24+
use std::cell::RefCell;
25+
implSolution{
26+
pubfnkth_smallest(root:Option<Rc<RefCell<TreeNode>>>,k:i32) ->i32{
27+
letmut vec =vec![];
28+
Self::in_order(&root,&mut vec);
29+
vec[kasusize -1]
30+
}
31+
32+
pubfnin_order(root:&Option<Rc<RefCell<TreeNode>>>,vec:&mutVec<i32>){
33+
ifletSome(node) = root{
34+
let _ =Self::in_order(&node.borrow().left, vec);
35+
vec.push(node.borrow().val);
36+
let _ =Self::in_order(&node.borrow().right, vec);
37+
}
38+
}
39+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
230\. Kth Smallest Element in a BST
2+
3+
Medium
4+
5+
Given the`root` of a binary search tree, and an integer`k`, return_the_ <code>k<sup>th</sup></code>_smallest value (**1-indexed**) of all the values of the nodes in the tree_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg)
10+
11+
**Input:** root =[3,1,4,null,2], k = 1
12+
13+
**Output:** 1
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg)
18+
19+
**Input:** root =[5,3,6,2,4,null,null,1], k = 3
20+
21+
**Output:** 3
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the tree is`n`.
26+
* <code>1 <= k <= n <= 10<sup>4</sup></code>
27+
* <code>0 <= Node.val <= 10<sup>4</sup></code>
28+
29+
**Follow up:** If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// #Easy #Top_100_Liked_Questions #Two_Pointers #Stack #Linked_List #Recursion
2+
// #Level_2_Day_3_Linked_List #Udemy_Linked_List #Big_O_Time_O(n)_Space_O(1)
3+
// #2024_09_11_Time_43_ms_(85.29%)_Space_9.2_MB_(34.31%)
4+
5+
// Definition for singly-linked list.
6+
// #[derive(PartialEq, Eq, Clone, Debug)]
7+
// pub struct ListNode {
8+
// pub val: i32,
9+
// pub next: Option<Box<ListNode>>
10+
// }
11+
//
12+
// impl ListNode {
13+
// #[inline]
14+
// fn new(val: i32) -> Self {
15+
// ListNode {
16+
// next: None,
17+
// val
18+
// }
19+
// }
20+
// }
21+
implSolution{
22+
pubfnis_palindrome(head:Option<Box<ListNode>>) ->bool{
23+
letmut ref_node =&head;
24+
letmut len =0;
25+
26+
whileletSome(ref node) =&ref_node{
27+
ref_node =&node.next;
28+
len +=1;
29+
}
30+
31+
ref_node =&head;
32+
letmut i =0;
33+
letmut num =0;
34+
letmut skip_num =if len %2 ==1{ len /2 +1}else{0};
35+
36+
whileletSome(node) = ref_node{
37+
ref_node =&node.next;
38+
39+
if skip_num >0 && skip_num -1 == i{
40+
skip_num =0;
41+
continue;
42+
}
43+
44+
if i < len /2{
45+
num = num*10 + node.valasusize;
46+
}else{
47+
num -= node.valasusize*10_usize.pow(i - len /2);
48+
}
49+
i +=1;
50+
}
51+
52+
num ==0
53+
}
54+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
234\. Palindrome Linked List
2+
3+
Easy
4+
5+
Given the`head` of a singly linked list, return`true`_if it is a palindrome or_`false`_otherwise_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg)
10+
11+
**Input:** head =[1,2,2,1]
12+
13+
**Output:** true
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg)
18+
19+
**Input:** head =[1,2]
20+
21+
**Output:** false
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.
26+
*`0 <= Node.val <= 9`
27+
28+
**Follow up:** Could you do it in`O(n)` time and`O(1)` space?

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp