Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 50da6d4

Browse files
author
guangsheng.li01
committed
Solved 0145
1 parent e06a49e commit 50da6d4

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* [0145] binary-tree-postorder-traversal
3+
*/
4+
5+
pub struct Solution {}
6+
use super::utils::tree::TreeNode;
7+
8+
// solution impl starts here
9+
10+
// Definition for a binary tree node.
11+
// #[derive(Debug, PartialEq, Eq)]
12+
// pub struct TreeNode {
13+
// pub val: i32,
14+
// pub left: Option<Rc<RefCell<TreeNode>>>,
15+
// pub right: Option<Rc<RefCell<TreeNode>>>,
16+
// }
17+
//
18+
// impl TreeNode {
19+
// #[inline]
20+
// pub fn new(val: i32) -> Self {
21+
// TreeNode {
22+
// val,
23+
// left: None,
24+
// right: None
25+
// }
26+
// }
27+
// }
28+
use std::cell::RefCell;
29+
use std::rc::Rc;
30+
impl Solution {
31+
pub fn postorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
32+
let mut res: Vec<i32> = Vec::new();
33+
Solution::travel(&root, &mut res);
34+
res
35+
}
36+
37+
pub fn travel(root: &Option<Rc<RefCell<TreeNode>>>, v: &mut Vec<i32>) {
38+
if root.is_none() {
39+
return;
40+
}
41+
42+
if let Some(r) = root {
43+
let b = r.borrow();
44+
if !b.left.is_none() {
45+
Solution::travel(&b.left, v);
46+
}
47+
48+
if !b.right.is_none() {
49+
Solution::travel(&b.right, v);
50+
}
51+
52+
v.push(b.val);
53+
}
54+
}
55+
}
56+
57+
// solution impl ends here
58+
59+
// solution tests starts here
60+
61+
#[cfg(test)]
62+
mod tests {
63+
use super::*;
64+
65+
#[test]
66+
fn test_case0() {
67+
assert_eq!(
68+
Solution::postorder_traversal(tree![1, null, 2, 3]),
69+
vec![3, 2, 1]
70+
);
71+
}
72+
}
73+
74+
// solution tests ends here

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod a0000_sample;
77
mod a0001_two_sum;
88
mod a0007_reverse_integer;
99
mod a0009_palindrome_number;
10+
mod a0145_binary_tree_postorder_traversal;
1011
mod a0172_factorial_trailing_zeroes;
1112
mod a0400_nth_digit;
1213
mod a0404_sum_of_left_leaves;

0 commit comments

Comments
 (0)