diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 4a177f6..0ce5d94 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -50,4 +50,5 @@ mod p987_vertical_order_traversal_of_a_binary_tree; mod p102_binary_tree_level_order_traversal; mod p107_binary_tree_level_order_traversal_ii; mod p103_binary_tree_zigzag_level_order_traversal; -mod p105_construct_binary_tree_from_preorder_and_inorder_traversal; \ No newline at end of file +mod p105_construct_binary_tree_from_preorder_and_inorder_traversal; +mod p106_construct_binary_tree_from_inorder_and_postorder_traversal; \ No newline at end of file diff --git a/src/problem/p106_construct_binary_tree_from_inorder_and_postorder_traversal.rs b/src/problem/p106_construct_binary_tree_from_inorder_and_postorder_traversal.rs new file mode 100644 index 0000000..55333fa --- /dev/null +++ b/src/problem/p106_construct_binary_tree_from_inorder_and_postorder_traversal.rs @@ -0,0 +1,98 @@ +/** + * [106] Construct Binary Tree from Inorder and Postorder Traversal + */ +pub struct Solution {} + +use crate::util::tree::{to_tree, TreeNode}; + +// submission codes start here + +// Definition for a binary tree 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 +// } +// } +// } +use std::cell::RefCell; +use std::rc::Rc; +impl Solution { + pub fn build_tree(inorder: Vec, postorder: Vec) -> Option>> { + if inorder.len() == 0 { + return None; + } + + let root = Rc::new(RefCell::new(TreeNode::new(postorder[postorder.len() - 1]))); + + let mut stack = Vec::new(); + stack.push(Rc::clone(&root)); + + let mut inorder_index = inorder.len() - 1; + + for i in (0..postorder.len() - 1).rev() { + let post_order_value = postorder[i]; + let mut node = Rc::clone(stack.last().unwrap()); + + if node.borrow().val != inorder[inorder_index] { + let new_node = Rc::new(RefCell::new(TreeNode::new(post_order_value))); + stack.push(Rc::clone(&new_node)); + node.borrow_mut().right = Some(new_node); + } else { + while let Some(top) = stack.last() { + if top.borrow().val == inorder[inorder_index] { + node = Rc::clone(top); + stack.pop(); + inorder_index -= 1; + } else { + break; + } + } + + let new_node = Rc::new(RefCell::new(TreeNode::new(post_order_value))); + stack.push(Rc::clone(&new_node)); + node.borrow_mut().left = Some(new_node); + } + } + + Some(root) + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_106() { + assert_eq!( + Solution::build_tree(vec![9, 3, 15, 20, 7], vec![9, 15, 7, 20, 3]), + to_tree(vec![ + Some(3), + Some(9), + Some(20), + None, + None, + Some(15), + Some(7) + ]) + ); + + assert_eq!( + Solution::build_tree(vec![-1], vec![-1]), + to_tree(vec![Some(-1)]) + ); + } +}