From f056b7ece6be055f1f046869ee11c37cafaadd7d Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sun, 4 Dec 2022 16:49:13 +0800 Subject: [PATCH] =?UTF-8?q?Update=200236.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1=E7=A5=96=E5=85=88?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0236.二叉树的最近公共祖先.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0236.二叉树的最近公共祖先.md b/problems/0236.二叉树的最近公共祖先.md index a34e8c21..eadae35e 100644 --- a/problems/0236.二叉树的最近公共祖先.md +++ b/problems/0236.二叉树的最近公共祖先.md @@ -384,6 +384,34 @@ object Solution { } ``` +## rust + +```rust +impl Solution { + pub fn lowest_common_ancestor( + root: Option>>, + p: Option>>, + q: Option>>, + ) -> Option>> { + if root == p || root == q || root.is_none() { + return root; + } + let left = Self::lowest_common_ancestor( + root.as_ref().unwrap().borrow().left.clone(), + p.clone(), + q.clone(), + ); + let right = + Self::lowest_common_ancestor(root.as_ref().unwrap().borrow().right.clone(), p, q); + match (&left, &right) { + (None, Some(_)) => right, + (Some(_), Some(_)) => root, + _ => left, + } + } +} +``` +