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)))) + } + } +} +``` +