Files
LeetCode-Go/leetcode/0701.Insert-into-a-Binary-Search-Tree/701. Insert into a Binary Search Tree.go
2022-09-10 16:41:11 -07:00

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