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] =?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 + } +} +```