@@ -383,6 +383,98 @@ func findBottomLeftValue(root *TreeNode) int {
383
383
}
384
384
```
385
385
386
+ ### ** Rust**
387
+
388
+ ``` rust
389
+ // Definition for a binary tree node.
390
+ // #[derive(Debug, PartialEq, Eq)]
391
+ // pub struct TreeNode {
392
+ // pub val: i32,
393
+ // pub left: Option<Rc<RefCell<TreeNode>>>,
394
+ // pub right: Option<Rc<RefCell<TreeNode>>>,
395
+ // }
396
+ //
397
+ // impl TreeNode {
398
+ // #[inline]
399
+ // pub fn new(val: i32) -> Self {
400
+ // TreeNode {
401
+ // val,
402
+ // left: None,
403
+ // right: None
404
+ // }
405
+ // }
406
+ // }
407
+ use std :: rc :: Rc ;
408
+ use std :: cell :: RefCell ;
409
+ use std :: collections :: VecDeque ;
410
+ impl Solution {
411
+ pub fn find_bottom_left_value (root : Option <Rc <RefCell <TreeNode >>>) -> i32 {
412
+ let mut queue = VecDeque :: new ();
413
+ queue . push_back (root );
414
+ let mut res = 0 ;
415
+ while ! queue . is_empty () {
416
+ res = queue . front (). unwrap (). as_ref (). unwrap (). borrow_mut (). val;
417
+ for _ in 0 .. queue . len () {
418
+ let node = queue . pop_front (). unwrap ();
419
+ let mut node = node . as_ref (). unwrap (). borrow_mut ();
420
+ if node . left. is_some () {
421
+ queue . push_back (node . left. take ());
422
+ }
423
+ if node . right. is_some () {
424
+ queue . push_back (node . right. take ());
425
+ }
426
+ }
427
+ }
428
+ res
429
+ }
430
+ }
431
+ ```
432
+
433
+ ``` rust
434
+ // Definition for a binary tree node.
435
+ // #[derive(Debug, PartialEq, Eq)]
436
+ // pub struct TreeNode {
437
+ // pub val: i32,
438
+ // pub left: Option<Rc<RefCell<TreeNode>>>,
439
+ // pub right: Option<Rc<RefCell<TreeNode>>>,
440
+ // }
441
+ //
442
+ // impl TreeNode {
443
+ // #[inline]
444
+ // pub fn new(val: i32) -> Self {
445
+ // TreeNode {
446
+ // val,
447
+ // left: None,
448
+ // right: None
449
+ // }
450
+ // }
451
+ // }
452
+ use std :: rc :: Rc ;
453
+ use std :: cell :: RefCell ;
454
+ use std :: collections :: VecDeque ;
455
+ impl Solution {
456
+ fn dfs (root : & Option <Rc <RefCell <TreeNode >>>, cur : i32 , max : & mut i32 , res : & mut i32 ) {
457
+ if root . is_none () {
458
+ return ;
459
+ }
460
+ let root = root . as_ref (). unwrap (). borrow ();
461
+ Self :: dfs (& root . left, cur + 1 , max , res );
462
+ Self :: dfs (& root . right, cur + 1 , max , res );
463
+ if * max < cur {
464
+ * max = cur ;
465
+ * res = root . val;
466
+ }
467
+ }
468
+
469
+ pub fn find_bottom_left_value (root : Option <Rc <RefCell <TreeNode >>>) -> i32 {
470
+ let mut max = 0 ;
471
+ let mut res = 0 ;
472
+ Self :: dfs (& root , 1 , & mut max ,& mut res );
473
+ res
474
+ }
475
+ }
476
+ ```
477
+
386
478
### ** ...**
387
479
388
480
```
0 commit comments