From 05baa4da0b87fd9d1c3fe0ad820c3bd22f98ded5 Mon Sep 17 00:00:00 2001 From: jtr109 Date: Tue, 11 Aug 2020 11:22:27 +0800 Subject: [PATCH 1/6] add document command --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) 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. From 13d4fb9c283b28d33a1dcc3cb83d89fb77fe1dba Mon Sep 17 00:00:00 2001 From: jtr109 Date: Tue, 11 Aug 2020 11:46:01 +0800 Subject: [PATCH 2/6] init new package --- Cargo.lock | 4 ++++ Cargo.toml | 1 + binary_tree_postorder_traversal/Cargo.toml | 9 +++++++++ binary_tree_postorder_traversal/src/main.rs | 3 +++ 4 files changed, 17 insertions(+) create mode 100644 binary_tree_postorder_traversal/Cargo.toml create mode 100644 binary_tree_postorder_traversal/src/main.rs 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/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/main.rs b/binary_tree_postorder_traversal/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/binary_tree_postorder_traversal/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} From 70045db5c2066e80e31499dd04221e9c7ca24d9a Mon Sep 17 00:00:00 2001 From: jtr109 Date: Tue, 11 Aug 2020 12:15:40 +0800 Subject: [PATCH 3/6] explain what is postorder traversal --- binary_tree_postorder_traversal/src/lib.rs | 46 +++++++++++++++++++++ binary_tree_postorder_traversal/src/main.rs | 3 -- 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 binary_tree_postorder_traversal/src/lib.rs delete mode 100644 binary_tree_postorder_traversal/src/main.rs diff --git a/binary_tree_postorder_traversal/src/lib.rs b/binary_tree_postorder_traversal/src/lib.rs new file mode 100644 index 0000000..4edbdcb --- /dev/null +++ b/binary_tree_postorder_traversal/src/lib.rs @@ -0,0 +1,46 @@ +/*! + * # 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. + */ + +#[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 { + vec![] + } +} diff --git a/binary_tree_postorder_traversal/src/main.rs b/binary_tree_postorder_traversal/src/main.rs deleted file mode 100644 index e7a11a9..0000000 --- a/binary_tree_postorder_traversal/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("Hello, world!"); -} From b3a9934e8cc2fc5321d49740973d7f53b6ed4eee Mon Sep 17 00:00:00 2001 From: jtr109 Date: Tue, 11 Aug 2020 13:47:18 +0800 Subject: [PATCH 4/6] add example test --- binary_tree_postorder_traversal/src/lib.rs | 30 +++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/binary_tree_postorder_traversal/src/lib.rs b/binary_tree_postorder_traversal/src/lib.rs index 4edbdcb..670b47c 100644 --- a/binary_tree_postorder_traversal/src/lib.rs +++ b/binary_tree_postorder_traversal/src/lib.rs @@ -14,6 +14,8 @@ * > 3. Access the data part of the current node. */ +#![allow(dead_code)] + #[derive(Debug, PartialEq, Eq)] pub struct TreeNode { pub val: i32, @@ -41,6 +43,32 @@ use std::rc::Rc; impl Solution { pub fn postorder_traversal(root: Option>>) -> Vec { - vec![] + vec![3, 2, 1] + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_example() { + let three = Some(Rc::new(RefCell::new(TreeNode { + val: 3, + left: None, + right: None, + }))); + 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); } } From 77c33892f0d75b168cf77e4c4f14b22fad34720d Mon Sep 17 00:00:00 2001 From: jtr109 Date: Tue, 11 Aug 2020 15:44:40 +0800 Subject: [PATCH 5/6] add the solution --- binary_tree_postorder_traversal/src/lib.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/binary_tree_postorder_traversal/src/lib.rs b/binary_tree_postorder_traversal/src/lib.rs index 670b47c..8a009ac 100644 --- a/binary_tree_postorder_traversal/src/lib.rs +++ b/binary_tree_postorder_traversal/src/lib.rs @@ -43,7 +43,18 @@ use std::rc::Rc; impl Solution { pub fn postorder_traversal(root: Option>>) -> Vec { - vec![3, 2, 1] + 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(); + } + } } } From edd9881c1c55f76776dc928b963e81f26da54640 Mon Sep 17 00:00:00 2001 From: jtr109 Date: Tue, 11 Aug 2020 15:54:16 +0800 Subject: [PATCH 6/6] new TreeNode --- binary_tree_postorder_traversal/src/lib.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/binary_tree_postorder_traversal/src/lib.rs b/binary_tree_postorder_traversal/src/lib.rs index 8a009ac..496b8fb 100644 --- a/binary_tree_postorder_traversal/src/lib.rs +++ b/binary_tree_postorder_traversal/src/lib.rs @@ -64,11 +64,7 @@ mod tests { #[test] fn test_example() { - let three = Some(Rc::new(RefCell::new(TreeNode { - val: 3, - left: None, - right: None, - }))); + let three = Some(Rc::new(RefCell::new(TreeNode::new(3)))); let two = Some(Rc::new(RefCell::new(TreeNode { val: 2, left: three,