From 334c9a8b0097bce9eeb7dfe5797ea1e89f60cb52 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+fwqaaq@users.noreply.github.com> Date: Fri, 9 Dec 2022 19:29:56 +0800 Subject: [PATCH] =?UTF-8?q?Update=200108.=E5=B0=86=E6=9C=89=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E8=BD=AC=E6=8D=A2=E4=B8=BA=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...将有序数组转换为二叉搜索树.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0108.将有序数组转换为二叉搜索树.md b/problems/0108.将有序数组转换为二叉搜索树.md index ffb60b38..0a053df5 100644 --- a/problems/0108.将有序数组转换为二叉搜索树.md +++ b/problems/0108.将有序数组转换为二叉搜索树.md @@ -482,6 +482,26 @@ object Solution { } ``` +## rust + +递归: + +```rust +impl Solution { + pub fn sorted_array_to_bst(nums: Vec) -> Option>> { + if nums.is_empty() { + return None; + } + let index = nums.len() / 2; + let mut root = TreeNode::new(nums[index]); + + root.left = Self::sorted_array_to_bst(nums[..index].to_vec()); + root.right = Self::sorted_array_to_bst(nums[index + 1..].to_vec()); + Some(Rc::new(RefCell::new(root))) + } +} +``` +