版权声明:原创勿转 https://cloud.tencent.com/developer/article/1419693
思路
如果插入值大于根节点,插入右子树
反之插入左子树
code
代码语言:javascript复制type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func insertIntoBST(root *TreeNode, val int) *TreeNode {
if root == nil {
return &TreeNode{
Val: val,
}
}
if val > root.Val {
root.Right = insertIntoBST(root.Right, val)
} else {
root.Left = insertIntoBST(root.Left, val)
}
return root
}