diff --git a/Cargo.lock b/Cargo.lock index 4c2f566..b112202 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,9 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "binary_tree_postorder_traversal" +version = "0.1.0" + [[package]] name = "kth_largest" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 0aadfb1..a735f0a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,5 @@ members = [ "kth_largest", "reverse_integer", "three_sum", + "binary_tree_postorder_traversal", ] diff --git a/README.md b/README.md index 32cbe3d..d717516 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,12 @@ Resolving problems of LeetCode in RustLang. * [15. 3 Sum](./three_sum/src/lib.rs) * [215. Kth Largest Element in an Array](./kth_largest/src/lib.rs) +## Document + +```shell +cargo doc --open -p +``` + ## References * [LeetcodeTop](https://github.com/afatcoder/LeetcodeTop): Top Hit Leetcode problems in interviews. diff --git a/binary_tree_postorder_traversal/Cargo.toml b/binary_tree_postorder_traversal/Cargo.toml new file mode 100644 index 0000000..31326b8 --- /dev/null +++ b/binary_tree_postorder_traversal/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "binary_tree_postorder_traversal" +version = "0.1.0" +authors = ["Ryan Li "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/binary_tree_postorder_traversal/src/lib.rs b/binary_tree_postorder_traversal/src/lib.rs new file mode 100644 index 0000000..496b8fb --- /dev/null +++ b/binary_tree_postorder_traversal/src/lib.rs @@ -0,0 +1,81 @@ +/*! + * # 145. Binary Tree Postorder Traversal + * + * * [problem link](https://leetcode.com/problems/binary-tree-postorder-traversal/) + * + * ## What is Postorder Traversal + * + * We can find explanation in [Wiki](https://en.wikipedia.org/wiki/Tree_traversal#Post-order_(LRN)): + * + * > Post-order (LRN) + * > + * > 1. Traverse the left subtree by recursively calling the post-order function. + * > 2. Traverse the right subtree by recursively calling the post-order function. + * > 3. Access the data part of the current node. + */ + +#![allow(dead_code)] + +#[derive(Debug, PartialEq, Eq)] +pub struct TreeNode { + pub val: i32, + pub left: Option>>, + pub right: Option>>, +} + +impl TreeNode { + #[inline] + pub fn new(val: i32) -> Self { + TreeNode { + val, + left: None, + right: None, + } + } +} + +struct Solution {} + +// ---------------------------------------------------------------------------- + +use std::cell::RefCell; +use std::rc::Rc; + +impl Solution { + pub fn postorder_traversal(root: Option>>) -> Vec { + match root { + None => return vec![], + Some(n) => { + let left = n.borrow().left.clone(); + let right = n.borrow().right.clone(); + let val = n.borrow().val; + let left_vec = Self::postorder_traversal(left); + let right_vec = Self::postorder_traversal(right); + let val_vec = vec![val]; + return [left_vec, right_vec, val_vec].concat(); + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_example() { + let three = Some(Rc::new(RefCell::new(TreeNode::new(3)))); + let two = Some(Rc::new(RefCell::new(TreeNode { + val: 2, + left: three, + right: None, + }))); + let input = Some(Rc::new(RefCell::new(TreeNode { + val: 1, + left: None, + right: two, + }))); + let output = vec![3, 2, 1]; + assert_eq!(Solution::postorder_traversal(input), output); + } +}