diff --git a/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README.md b/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README.md index 7326a201dc439..387c2e1b0fb20 100644 --- a/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README.md +++ b/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README.md @@ -166,6 +166,60 @@ public: }; ``` +### **Rust** + +```rust +// 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::rc::Rc; +use std::cell::RefCell; +impl Solution { + #[allow(dead_code)] + pub fn flatten(root: &mut Option>>) { + if root.is_none() { + return; + } + let mut v: Vec>>> = Vec::new(); + // Initialize the vector + Self::pre_order_traverse(&mut v, root); + // Traverse the vector + let n = v.len(); + for i in 0..n - 1 { + v[i].as_ref().unwrap().borrow_mut().left = None; + v[i].as_ref().unwrap().borrow_mut().right = v[i + 1].clone(); + } + } + + #[allow(dead_code)] + fn pre_order_traverse(v: &mut Vec>>>, root: &Option>>) { + if root.is_none() { + return; + } + v.push(root.clone()); + let left = root.as_ref().unwrap().borrow().left.clone(); + let right = root.as_ref().unwrap().borrow().right.clone(); + Self::pre_order_traverse(v, &left); + Self::pre_order_traverse(v, &right); + } +} +``` + ### **Go** ```go diff --git a/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README_EN.md b/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README_EN.md index e97ffc7b434f9..d28488943419c 100644 --- a/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README_EN.md +++ b/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README_EN.md @@ -142,6 +142,60 @@ public: }; ``` +### **Rust** + +```rust +// 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::rc::Rc; +use std::cell::RefCell; +impl Solution { + #[allow(dead_code)] + pub fn flatten(root: &mut Option>>) { + if root.is_none() { + return; + } + let mut v: Vec>>> = Vec::new(); + // Initialize the vector + Self::pre_order_traverse(&mut v, root); + // Traverse the vector + let n = v.len(); + for i in 0..n - 1 { + v[i].as_ref().unwrap().borrow_mut().left = None; + v[i].as_ref().unwrap().borrow_mut().right = v[i + 1].clone(); + } + } + + #[allow(dead_code)] + fn pre_order_traverse(v: &mut Vec>>>, root: &Option>>) { + if root.is_none() { + return; + } + v.push(root.clone()); + let left = root.as_ref().unwrap().borrow().left.clone(); + let right = root.as_ref().unwrap().borrow().right.clone(); + Self::pre_order_traverse(v, &left); + Self::pre_order_traverse(v, &right); + } +} +``` + ### **Go** ```go diff --git a/solution/0100-0199/0114.Flatten Binary Tree to Linked List/Solution.rs b/solution/0100-0199/0114.Flatten Binary Tree to Linked List/Solution.rs new file mode 100644 index 0000000000000..b1d090e0a1ed3 --- /dev/null +++ b/solution/0100-0199/0114.Flatten Binary Tree to Linked List/Solution.rs @@ -0,0 +1,49 @@ +// 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::rc::Rc; +use std::cell::RefCell; +impl Solution { + #[allow(dead_code)] + pub fn flatten(root: &mut Option>>) { + if root.is_none() { + return; + } + let mut v: Vec>>> = Vec::new(); + // Initialize the vector + Self::pre_order_traverse(&mut v, root); + // Traverse the vector + let n = v.len(); + for i in 0..n - 1 { + v[i].as_ref().unwrap().borrow_mut().left = None; + v[i].as_ref().unwrap().borrow_mut().right = v[i + 1].clone(); + } + } + + #[allow(dead_code)] + fn pre_order_traverse(v: &mut Vec>>>, root: &Option>>) { + if root.is_none() { + return; + } + v.push(root.clone()); + let left = root.as_ref().unwrap().borrow().left.clone(); + let right = root.as_ref().unwrap().borrow().right.clone(); + Self::pre_order_traverse(v, &left); + Self::pre_order_traverse(v, &right); + } +} \ No newline at end of file