二叉树的删除为什么一定要返回更新后的子节点 ... //二叉树的删除 remove(key) { this.removeNode(this.root, key) } removeNode(node, key) { if (node == null) { return null } if (this.compareFn(key, node.key) === Compare.less) { //本人疑惑的地方//tip2 this.removeNode(node.left, key) } else if (this.compareFn(key, node.key) === Compare.bigger) { this.removeNode(node.right, key) } else { //tip1 node = null } } } var mytree = new BST() mytree.insert(4) mytree.insert(5) mytree.insert(1) mytree.remove(1) 我看到大家都是以下这么写二叉树的删除的,不明白的是我在tip2处将node.left传入removeNode函数之后,按照JavaScript复杂数据类型变量存的是栈中的地址,那么我在tip1处将key为1的这个节点赋值为null,也就是mytree.root.left这个节点赋值为Null之后,为什么它最终没有被赋值为null,而是要采用下面tip3的方式才能实现删除呢 removeNode(node,key){ if(node==null){ return null } if(this.compareFn(key,node.key)===Compare.less){ //都采用了赋值的方式,把更新后的子节点赋值给父节点要修改的地方 //tip3 node.left = this.removeNode(node.left,key) return node }else if(this.compareFn(key,node.key)===Compare.bigger){... 1.以下是比较官方的写法 const Compare = { less:-1, bigger:1, equ:0 } class Node{ constructor(key){ this.key = key this.left = null this.right = null } } class BST { constructor(){ this.root = null } insert(key){ if(this.root==null){ this.root = new Node(key) }else{ this.insertNode(this.root,key) } } compareFn(a,b){ if(a===b){ return Compare.equ } return a