编程语言
首页 > 编程语言> > [编程题] 向二叉搜索树中插入数据

[编程题] 向二叉搜索树中插入数据

作者:互联网

type BTree = TreeNode | null

class TreeNode {
    value: number
    left: BTree
    right: BTree

    constructor(value: number = 0, left: BTree = null, right: BTree = null) {
        this.value = value
        this.left = left
        this.right = right
    }
}

function insertNode(head: BTree, value: number): BTree {
    if (head === null) {
        head = new TreeNode(value)
        return head
    }
    if (head.value < value) {
        head.right = insertNode(head.right, value)
    } else {
        head.left = insertNode(head.left, value)
    }
    return head
}

标签:head,right,编程,null,value,二叉,BTree,树中,left
来源: https://www.cnblogs.com/toddforsure/p/insert-value-into-binary-search-tree.html