File tree 1 file changed +38
-0
lines changed
145-BinaryTreePostorderTraversal
1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
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
+
22
+ fn fill_vec ( root : & Option < Rc < RefCell < TreeNode > > > , vector : & mut Vec < i32 > ) {
23
+ if root. is_none ( ) {
24
+ return ;
25
+ }
26
+ let root = root. as_ref ( ) . unwrap ( ) . borrow ( ) ;
27
+ fill_vec ( & root. left , vector) ;
28
+ fill_vec ( & root. right , vector) ;
29
+ vector. push ( root. val ) ;
30
+ }
31
+
32
+ impl Solution {
33
+ pub fn postorder_traversal ( root : Option < Rc < RefCell < TreeNode > > > ) -> Vec < i32 > {
34
+ let mut result = Vec :: new ( ) ;
35
+ fill_vec ( & root, & mut result) ;
36
+ result
37
+ }
38
+ }
You can’t perform that action at this time.
0 commit comments