0
点赞
收藏
分享

微信扫一扫

力扣701. 二叉搜索树中的插入操作(JavaScript)

大漠雪关山月 2022-02-18 阅读 83
//如果比当前节点大,插在右子树,反之则插在左子树
var insertIntoBST = function(root, val) {
    if(root==null){
        let node=new TreeNode(val)
        return node
    }
    if(val>root.val){
        //插入新节点,则子树需更新
        root.right=insertIntoBST(root.right,val)
    }else{
        root.left=insertIntoBST(root.left,val)
    }
    return root
};
举报

相关推荐

0 条评论