mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-12 14:41:34 +08:00
Add solution 701
This commit is contained in:
@ -0,0 +1,30 @@
|
||||
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)
|
||||
}
|
Reference in New Issue
Block a user