Skip to content

Commit 5612f16

Browse files
committed
feat: add rust solution to lc problem: No.0144
No.0144.Binary Tree Preorder Traversal
1 parent 719a7d0 commit 5612f16

File tree

3 files changed

+209
-0
lines changed

3 files changed

+209
-0
lines changed

solution/0100-0199/0144.Binary Tree Preorder Traversal/README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,92 @@ func preorderTraversal(root *TreeNode) []int {
459459
}
460460
```
461461

462+
### **Rust**
463+
464+
```rust
465+
// Definition for a binary tree node.
466+
// #[derive(Debug, PartialEq, Eq)]
467+
// pub struct TreeNode {
468+
// pub val: i32,
469+
// pub left: Option<Rc<RefCell<TreeNode>>>,
470+
// pub right: Option<Rc<RefCell<TreeNode>>>,
471+
// }
472+
//
473+
// impl TreeNode {
474+
// #[inline]
475+
// pub fn new(val: i32) -> Self {
476+
// TreeNode {
477+
// val,
478+
// left: None,
479+
// right: None
480+
// }
481+
// }
482+
// }
483+
use std::rc::Rc;
484+
use std::cell::RefCell;
485+
impl Solution {
486+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
487+
if root.is_none() {
488+
return;
489+
}
490+
let node = root.as_ref().unwrap().borrow();
491+
res.push(node.val);
492+
Self::dfs(&node.left, res);
493+
Self::dfs(&node.right, res);
494+
}
495+
496+
pub fn preorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
497+
let mut res = vec![];
498+
Self::dfs(&root, &mut res);
499+
res
500+
}
501+
}
502+
```
503+
504+
```rust
505+
// Definition for a binary tree node.
506+
// #[derive(Debug, PartialEq, Eq)]
507+
// pub struct TreeNode {
508+
// pub val: i32,
509+
// pub left: Option<Rc<RefCell<TreeNode>>>,
510+
// pub right: Option<Rc<RefCell<TreeNode>>>,
511+
// }
512+
//
513+
// impl TreeNode {
514+
// #[inline]
515+
// pub fn new(val: i32) -> Self {
516+
// TreeNode {
517+
// val,
518+
// left: None,
519+
// right: None
520+
// }
521+
// }
522+
// }
523+
use std::rc::Rc;
524+
use std::cell::RefCell;
525+
impl Solution {
526+
pub fn preorder_traversal(mut root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
527+
let mut res = vec![];
528+
if root.is_none() {
529+
return res;
530+
}
531+
let mut stack = vec![];
532+
while root.is_some() || stack.len() != 0 {
533+
if root.is_some() {
534+
let val = root.as_ref().unwrap().as_ref().borrow().val;
535+
let left = root.as_ref().unwrap().as_ref().borrow_mut().left.take();
536+
res.push(val);
537+
stack.push(root);
538+
root = left;
539+
} else {
540+
root = stack.pop().unwrap().as_ref().unwrap().as_ref().borrow_mut().right.take();
541+
}
542+
}
543+
res
544+
}
545+
}
546+
```
547+
462548
### **...**
463549

464550
```

solution/0100-0199/0144.Binary Tree Preorder Traversal/README_EN.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,92 @@ func preorderTraversal(root *TreeNode) []int {
413413
}
414414
```
415415

416+
### **Rust**
417+
418+
```rust
419+
// Definition for a binary tree node.
420+
// #[derive(Debug, PartialEq, Eq)]
421+
// pub struct TreeNode {
422+
// pub val: i32,
423+
// pub left: Option<Rc<RefCell<TreeNode>>>,
424+
// pub right: Option<Rc<RefCell<TreeNode>>>,
425+
// }
426+
//
427+
// impl TreeNode {
428+
// #[inline]
429+
// pub fn new(val: i32) -> Self {
430+
// TreeNode {
431+
// val,
432+
// left: None,
433+
// right: None
434+
// }
435+
// }
436+
// }
437+
use std::rc::Rc;
438+
use std::cell::RefCell;
439+
impl Solution {
440+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
441+
if root.is_none() {
442+
return;
443+
}
444+
let node = root.as_ref().unwrap().borrow();
445+
res.push(node.val);
446+
Self::dfs(&node.left, res);
447+
Self::dfs(&node.right, res);
448+
}
449+
450+
pub fn preorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
451+
let mut res = vec![];
452+
Self::dfs(&root, &mut res);
453+
res
454+
}
455+
}
456+
```
457+
458+
```rust
459+
// Definition for a binary tree node.
460+
// #[derive(Debug, PartialEq, Eq)]
461+
// pub struct TreeNode {
462+
// pub val: i32,
463+
// pub left: Option<Rc<RefCell<TreeNode>>>,
464+
// pub right: Option<Rc<RefCell<TreeNode>>>,
465+
// }
466+
//
467+
// impl TreeNode {
468+
// #[inline]
469+
// pub fn new(val: i32) -> Self {
470+
// TreeNode {
471+
// val,
472+
// left: None,
473+
// right: None
474+
// }
475+
// }
476+
// }
477+
use std::rc::Rc;
478+
use std::cell::RefCell;
479+
impl Solution {
480+
pub fn preorder_traversal(mut root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
481+
let mut res = vec![];
482+
if root.is_none() {
483+
return res;
484+
}
485+
let mut stack = vec![];
486+
while root.is_some() || stack.len() != 0 {
487+
if root.is_some() {
488+
let val = root.as_ref().unwrap().as_ref().borrow().val;
489+
let left = root.as_ref().unwrap().as_ref().borrow_mut().left.take();
490+
res.push(val);
491+
stack.push(root);
492+
root = left;
493+
} else {
494+
root = stack.pop().unwrap().as_ref().unwrap().as_ref().borrow_mut().right.take();
495+
}
496+
}
497+
res
498+
}
499+
}
500+
```
501+
416502
### **...**
417503

418504
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
impl Solution {
22+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
23+
if root.is_none() {
24+
return;
25+
}
26+
let node = root.as_ref().unwrap().borrow();
27+
res.push(node.val);
28+
Self::dfs(&node.left, res);
29+
Self::dfs(&node.right, res);
30+
}
31+
32+
pub fn preorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
33+
let mut res = vec![];
34+
Self::dfs(&root, &mut res);
35+
res
36+
}
37+
}

0 commit comments

Comments
 (0)