From 3b3ed756664435b73fceb20a971fa8ef03d608f8 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 26 Nov 2022 15:16:06 +0800 Subject: [PATCH 1/4] =?UTF-8?q?Update=200617.=E5=90=88=E5=B9=B6=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0617.合并二叉树.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/0617.合并二叉树.md b/problems/0617.合并二叉树.md index db2d3762..f0ecf79b 100644 --- a/problems/0617.合并二叉树.md +++ b/problems/0617.合并二叉树.md @@ -691,6 +691,35 @@ object Solution { } ``` +### rust + +递归: + +```rust +use std::cell::RefCell; +use std::rc::Rc; +impl Solution { + pub fn merge_trees( + root1: Option>>, + root2: Option>>, + ) -> Option>> { + if root1.is_none() { + return root2; + } + if root2.is_none() { + return root1; + } + let binding = root1.clone(); + let mut node1 = binding.as_ref().unwrap().borrow_mut(); + let node2 = root2.as_ref().unwrap().borrow_mut(); + node1.left = Self::merge_trees(node1.left.clone(), node2.left.clone()); + node1.right = Self::merge_trees(node1.right.clone(), node2.right.clone()); + node1.val += node2.val; + + root1 + } +} +```

From ae99d647ac6f662a9a97340511c07e541c2f257a Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 26 Nov 2022 16:41:16 +0800 Subject: [PATCH 2/4] =?UTF-8?q?Update=200617.=E5=90=88=E5=B9=B6=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0617.合并二叉树.md | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/problems/0617.合并二叉树.md b/problems/0617.合并二叉树.md index f0ecf79b..9fe457c6 100644 --- a/problems/0617.合并二叉树.md +++ b/problems/0617.合并二叉树.md @@ -721,6 +721,49 @@ impl Solution { } ``` +迭代: + +```rust +impl Solution { + pub fn merge_trees( + root1: Option>>, + root2: Option>>, + ) -> Option>> { + if root1.is_none() { + return root2; + } + if root2.is_none() { + return root1; + } + let mut stack = vec![]; + stack.push(root2); + stack.push(root1.clone()); + while !stack.is_empty() { + let node1 = stack.pop().unwrap().unwrap(); + let node2 = stack.pop().unwrap().unwrap(); + let mut node1 = node1.borrow_mut(); + let node2 = node2.borrow(); + node1.val += node2.val; + if node1.left.is_some() && node2.left.is_some() { + stack.push(node2.left.clone()); + stack.push(node1.left.clone()); + } + if node1.right.is_some() && node2.right.is_some() { + stack.push(node2.right.clone()); + stack.push(node1.right.clone()); + } + if node1.left.is_none() && node2.left.is_some() { + node1.left = node2.left.clone(); + } + if node1.right.is_none() && node2.right.is_some() { + node1.right = node2.right.clone(); + } + } + root1 + } +} +``` +

From 5c4b8b3d8140ecc67f917f49899d77ee49f9b83d Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 26 Nov 2022 18:07:46 +0800 Subject: [PATCH 3/4] =?UTF-8?q?Update=200700.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=90=9C=E7=B4=A2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0700.二叉搜索树中的搜索.md | 27 ++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0700.二叉搜索树中的搜索.md b/problems/0700.二叉搜索树中的搜索.md index 2ee2bdb0..a0143bd8 100644 --- a/problems/0700.二叉搜索树中的搜索.md +++ b/problems/0700.二叉搜索树中的搜索.md @@ -414,6 +414,33 @@ object Solution { } ``` +### rust + +递归: + +```rust +use std::cell::RefCell; +use std::rc::Rc; +impl Solution { + pub fn search_bst( + root: Option>>, + val: i32, + ) -> Option>> { + if root.is_none() || root.as_ref().unwrap().borrow().val == val { + return root; + } + let node_val = root.as_ref().unwrap().borrow().val; + if node_val > val { + return Self::search_bst(root.as_ref().unwrap().borrow().left.clone(), val); + } + if node_val < val { + return Self::search_bst(root.unwrap().borrow().right.clone(), val); + } + None + } +} +``` +

From 4be111b3d170b7f3ff5888d0cb343368818e4fd2 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 26 Nov 2022 18:25:55 +0800 Subject: [PATCH 4/4] =?UTF-8?q?Update=200700.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=90=9C=E7=B4=A2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0700.二叉搜索树中的搜索.md | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0700.二叉搜索树中的搜索.md b/problems/0700.二叉搜索树中的搜索.md index a0143bd8..c09f11b9 100644 --- a/problems/0700.二叉搜索树中的搜索.md +++ b/problems/0700.二叉搜索树中的搜索.md @@ -441,6 +441,29 @@ impl Solution { } ``` +迭代: + +```rust +use std::cell::RefCell; +use std::rc::Rc; +use std::cmp; +impl Solution { + pub fn search_bst( + mut root: Option>>, + val: i32, + ) -> Option>> { + while let Some(ref node) = root.clone() { + match val.cmp(&node.borrow().val) { + cmp::Ordering::Less => root = node.borrow().left.clone(), + cmp::Ordering::Equal => return root, + cmp::Ordering::Greater => root = node.borrow().right.clone(), + }; + } + None + } +} +``` +