mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 09:23:19 +08:00
31 lines
549 B
Go
31 lines
549 B
Go
package leetcode
|
|
|
|
import "github.com/halfrost/LeetCode-Go/structures"
|
|
|
|
// TreeNode define
|
|
type TreeNode = structures.TreeNode
|
|
|
|
/**
|
|
* Definition for a binary tree node.
|
|
* type TreeNode struct {
|
|
* Val int
|
|
* Left *TreeNode
|
|
* Right *TreeNode
|
|
* }
|
|
*/
|
|
func insert(n *TreeNode, val int) *TreeNode {
|
|
if n == nil {
|
|
return &TreeNode{Val: val}
|
|
}
|
|
if n.Val < val {
|
|
n.Right = insert(n.Right, val)
|
|
} else {
|
|
n.Left = insert(n.Left, val)
|
|
}
|
|
return n
|
|
}
|
|
|
|
func insertIntoBST(root *TreeNode, val int) *TreeNode {
|
|
return insert(root, val)
|
|
}
|