1
- use super :: utils;
1
+ use super :: utils:: * ;
2
2
use std:: cell:: RefCell ;
3
3
use std:: rc:: Rc ;
4
4
@@ -103,20 +103,20 @@ pub fn valid_parentheses(s: String) -> bool {
103
103
}
104
104
105
105
pub fn merge_two_sorted_lists (
106
- list1 : Option < Box < utils :: ListNode > > ,
107
- list2 : Option < Box < utils :: ListNode > > ,
108
- ) -> Option < Box < utils :: ListNode > > {
106
+ list1 : Option < Box < ListNode > > ,
107
+ list2 : Option < Box < ListNode > > ,
108
+ ) -> Option < Box < ListNode > > {
109
109
match ( list1, list2) {
110
110
( None , None ) => None ,
111
111
( Some ( n) , None ) | ( None , Some ( n) ) => Some ( n) ,
112
112
( Some ( list1) , Some ( list2) ) => {
113
113
if list1. val >= list2. val {
114
- Some ( Box :: new ( utils :: ListNode {
114
+ Some ( Box :: new ( ListNode {
115
115
val : list2. val ,
116
116
next : merge_two_sorted_lists ( Some ( list1) , list2. next ) ,
117
117
} ) )
118
118
} else {
119
- Some ( Box :: new ( utils :: ListNode {
119
+ Some ( Box :: new ( ListNode {
120
120
val : list1. val ,
121
121
next : merge_two_sorted_lists ( list1. next , Some ( list2) ) ,
122
122
} ) )
@@ -221,9 +221,7 @@ pub fn climbing_stairs(n: i32) -> i32 {
221
221
( 0 ..n) . fold ( ( 1 , 0 ) , |( res, prev) , _| ( res + prev, res) ) . 0
222
222
}
223
223
224
- pub fn remove_duplicates_from_sorted_list (
225
- head : Option < Box < utils:: ListNode > > ,
226
- ) -> Option < Box < utils:: ListNode > > {
224
+ pub fn remove_duplicates_from_sorted_list ( head : Option < Box < ListNode > > ) -> Option < Box < ListNode > > {
227
225
head. map ( |mut ln| {
228
226
let mut cur = ln. as_mut ( ) ;
229
227
while let Some ( next) = cur. next . as_mut ( ) {
@@ -250,9 +248,9 @@ pub fn merge_sorted_array(nums1: &mut Vec<i32>, m: i32, nums2: &mut Vec<i32>, n:
250
248
}
251
249
}
252
250
253
- pub fn binary_tree_inorder_traversal ( root : Option < Rc < RefCell < utils :: TreeNode > > > ) -> Vec < i32 > {
251
+ pub fn binary_tree_inorder_traversal ( root : Option < Rc < RefCell < TreeNode > > > ) -> Vec < i32 > {
254
252
let mut curr = root;
255
- let mut stack = Vec :: < Rc < RefCell < utils :: TreeNode > > > :: new ( ) ;
253
+ let mut stack = Vec :: < Rc < RefCell < TreeNode > > > :: new ( ) ;
256
254
let mut res = vec ! [ ] ;
257
255
258
256
while curr. is_some ( ) || !stack. is_empty ( ) {
@@ -270,14 +268,11 @@ pub fn binary_tree_inorder_traversal(root: Option<Rc<RefCell<utils::TreeNode>>>)
270
268
res
271
269
}
272
270
273
- pub fn same_tree (
274
- p : Option < Rc < RefCell < utils:: TreeNode > > > ,
275
- q : Option < Rc < RefCell < utils:: TreeNode > > > ,
276
- ) -> bool {
271
+ pub fn same_tree ( p : Option < Rc < RefCell < TreeNode > > > , q : Option < Rc < RefCell < TreeNode > > > ) -> bool {
277
272
p == q
278
273
}
279
274
280
- pub fn symmetric_tree ( root : Option < Rc < RefCell < utils :: TreeNode > > > ) -> bool {
275
+ pub fn symmetric_tree ( root : Option < Rc < RefCell < TreeNode > > > ) -> bool {
281
276
if root. is_none ( ) {
282
277
return true ;
283
278
}
@@ -301,7 +296,7 @@ pub fn symmetric_tree(root: Option<Rc<RefCell<utils::TreeNode>>>) -> bool {
301
296
true
302
297
}
303
298
304
- pub fn maximum_depth_of_binary_tree ( root : Option < Rc < RefCell < utils :: TreeNode > > > ) -> i32 {
299
+ pub fn maximum_depth_of_binary_tree ( root : Option < Rc < RefCell < TreeNode > > > ) -> i32 {
305
300
if root. is_none ( ) {
306
301
return 0 ;
307
302
}
@@ -322,16 +317,14 @@ pub fn maximum_depth_of_binary_tree(root: Option<Rc<RefCell<utils::TreeNode>>>)
322
317
max_depth
323
318
}
324
319
325
- pub fn convert_sorted_array_to_binary_search_tree (
326
- nums : Vec < i32 > ,
327
- ) -> Option < Rc < RefCell < utils:: TreeNode > > > {
320
+ pub fn convert_sorted_array_to_binary_search_tree ( nums : Vec < i32 > ) -> Option < Rc < RefCell < TreeNode > > > {
328
321
let n = nums. len ( ) ;
329
322
330
323
match n {
331
324
0 => None ,
332
325
_ => {
333
326
let m = n / 2 ;
334
- let mut node = utils :: TreeNode :: new ( nums[ m] ) ;
327
+ let mut node = TreeNode :: new ( nums[ m] ) ;
335
328
node. left = convert_sorted_array_to_binary_search_tree ( nums[ ..m] . to_vec ( ) ) ;
336
329
node. right = convert_sorted_array_to_binary_search_tree ( nums[ m + 1 ..] . to_vec ( ) ) ;
337
330
@@ -340,7 +333,7 @@ pub fn convert_sorted_array_to_binary_search_tree(
340
333
}
341
334
}
342
335
343
- pub fn balanced_binary_tree ( root : Option < Rc < RefCell < utils :: TreeNode > > > ) -> bool {
336
+ pub fn balanced_binary_tree ( root : Option < Rc < RefCell < TreeNode > > > ) -> bool {
344
337
let mut dstack = Vec :: new ( ) ;
345
338
let mut stack = Vec :: new ( ) ;
346
339
stack. push ( ( 1 as i32 , 0 as i32 , false , false , root) ) ;
@@ -379,7 +372,7 @@ pub fn balanced_binary_tree(root: Option<Rc<RefCell<utils::TreeNode>>>) -> bool
379
372
return true ;
380
373
}
381
374
382
- pub fn minimum_depth_of_binary_tree ( root : Option < Rc < RefCell < utils :: TreeNode > > > ) -> i32 {
375
+ pub fn minimum_depth_of_binary_tree ( root : Option < Rc < RefCell < TreeNode > > > ) -> i32 {
383
376
match root {
384
377
Some ( node) => {
385
378
let left = minimum_depth_of_binary_tree ( node. as_ref ( ) . borrow ( ) . left . clone ( ) ) ;
@@ -393,14 +386,14 @@ pub fn minimum_depth_of_binary_tree(root: Option<Rc<RefCell<utils::TreeNode>>>)
393
386
}
394
387
}
395
388
396
- pub fn path_sum ( root : Option < Rc < RefCell < utils :: TreeNode > > > , target_sum : i32 ) -> bool {
389
+ pub fn path_sum ( root : Option < Rc < RefCell < TreeNode > > > , target_sum : i32 ) -> bool {
397
390
root. map_or ( false , |root| match & * root. borrow ( ) {
398
- & utils :: TreeNode {
391
+ & TreeNode {
399
392
val,
400
393
left : None ,
401
394
right : None ,
402
395
} => val == target_sum,
403
- & utils :: TreeNode {
396
+ & TreeNode {
404
397
val,
405
398
ref left,
406
399
ref right,
@@ -430,11 +423,11 @@ pub fn single_number(nums: Vec<i32>) -> i32 {
430
423
431
424
// Linked List Cycle - not possible with current ListNode definition
432
425
433
- pub fn binary_tree_preorder_traversal ( root : Option < Rc < RefCell < utils :: TreeNode > > > ) -> Vec < i32 > {
426
+ pub fn binary_tree_preorder_traversal ( root : Option < Rc < RefCell < TreeNode > > > ) -> Vec < i32 > {
434
427
todo ! ( ) ;
435
428
}
436
429
437
- pub fn binary_tree_postorder_traversal ( root : Option < Rc < RefCell < utils :: TreeNode > > > ) -> Vec < i32 > {
430
+ pub fn binary_tree_postorder_traversal ( root : Option < Rc < RefCell < TreeNode > > > ) -> Vec < i32 > {
438
431
todo ! ( ) ;
439
432
}
440
433
@@ -470,18 +463,15 @@ pub fn happy_number(n: i32) -> bool {
470
463
todo ! ( ) ;
471
464
}
472
465
473
- pub fn remove_linked_list_elements (
474
- head : Option < Box < utils:: ListNode > > ,
475
- val : i32 ,
476
- ) -> Option < Box < utils:: ListNode > > {
466
+ pub fn remove_linked_list_elements ( head : Option < Box < ListNode > > , val : i32 ) -> Option < Box < ListNode > > {
477
467
todo ! ( ) ;
478
468
}
479
469
480
470
pub fn isomorphic_strings ( s : String , t : String ) -> bool {
481
471
todo ! ( ) ;
482
472
}
483
473
484
- pub fn reverse_linked_list ( head : Option < Box < utils :: ListNode > > ) -> Option < Box < utils :: ListNode > > {
474
+ pub fn reverse_linked_list ( head : Option < Box < ListNode > > ) -> Option < Box < ListNode > > {
485
475
todo ! ( ) ;
486
476
}
487
477
@@ -493,15 +483,13 @@ pub fn contains_duplicate_ii(nums: Vec<i32>, k: i32) -> bool {
493
483
todo ! ( ) ;
494
484
}
495
485
496
- pub fn count_complete_tree_nodes ( root : Option < Rc < RefCell < utils :: TreeNode > > > ) -> i32 {
486
+ pub fn count_complete_tree_nodes ( root : Option < Rc < RefCell < TreeNode > > > ) -> i32 {
497
487
todo ! ( ) ;
498
488
}
499
489
500
490
// Implement Stack using Queues - not applicable
501
491
502
- pub fn invert_binary_tree (
503
- root : Option < Rc < RefCell < utils:: TreeNode > > > ,
504
- ) -> Option < Rc < RefCell < utils:: TreeNode > > > {
492
+ pub fn invert_binary_tree ( root : Option < Rc < RefCell < TreeNode > > > ) -> Option < Rc < RefCell < TreeNode > > > {
505
493
todo ! ( ) ;
506
494
}
507
495
@@ -515,7 +503,7 @@ pub fn power_of_two(n: i32) -> bool {
515
503
516
504
// Implement Queue using Stacks - not applicable
517
505
518
- pub fn palindrome_linked_list ( head : Option < Box < utils :: ListNode > > ) -> bool {
506
+ pub fn palindrome_linked_list ( head : Option < Box < ListNode > > ) -> bool {
519
507
todo ! ( ) ;
520
508
}
521
509
0 commit comments