701. 二叉搜索树中的插入操作

题目链接:https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/

执行用时:188 ms, 在所有 Swift 提交中击败了95.12%的用户
内存消耗:14.8 MB, 在所有 Swift 提交中击败了19.51%的用户

递归

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
func insertIntoBST(_ root: TreeNode?, _ val: Int) -> TreeNode? {
if root == nil {
return TreeNode.init(val)
}
if val > root!.val {
root?.right = insertIntoBST(root?.right, val)
}
if val < root!.val {
root?.left = insertIntoBST(root?.left, val)
}
return root
}
}

701. 二叉搜索树中的插入操作
https://pisces34.github.io/2021/10/01/leetcode/701/
发布于
2021年10月1日
许可协议