From eb76f57a19e813e77490931d97787a4d8b4a0552 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+fwqaaq@users.noreply.github.com> Date: Mon, 5 Dec 2022 22:55:08 +0800 Subject: [PATCH] =?UTF-8?q?Update=200701.=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=8F=92=E5=85=A5=E6=93=8D?= =?UTF-8?q?=E4=BD=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0701.二叉搜索树中的插入操作.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md index b8d53b04..559c3900 100644 --- a/problems/0701.二叉搜索树中的插入操作.md +++ b/problems/0701.二叉搜索树中的插入操作.md @@ -677,6 +677,30 @@ impl Solution { } ``` +递归: + +```rust +impl Solution { + pub fn insert_into_bst( + root: Option>>, + val: i32, + ) -> Option>> { + if let Some(node) = &root { + if node.borrow().val > val { + let left = Self::insert_into_bst(node.borrow_mut().left.take(), val); + node.borrow_mut().left = left; + } else { + let right = Self::insert_into_bst(node.borrow_mut().right.take(), val); + node.borrow_mut().right = right; + } + root + } else { + Some(Rc::new(RefCell::new(TreeNode::new(val)))) + } + } +} +``` +