问答详情
源自:1-4 二叉树创建的代码实现

为什么我提示堆栈溢出?

function BinaryTree() {

var Node = function (key) {

this.key = key;

this.left = null;

this.right = null;

};

var root = null;

var insertNode = function (node, newNode) {

if (newNode.key < node.key) {

if (node.left === null) {

node.left = newNode;

} else {

insertNode(node.left, newNode);

}

} else {

if (node.right === null) {

node.right = newNode;

} else {

insertNode(node.right, newNode);

}

}

}


this.insert = function (key) {

var newNode = new Node(key);

if (root === null) {

root = newNode;

}

insertNode(root, newNode);

}

}


var nodes = [8, 3, 10, 1, 6, 14, 4, 7, 13];

var binaryTree = new BinaryTree();

nodes.forEach(function (key) {

binaryTree.insert(key);

//console.log(binaryTree);

})



提问者:MMD_野孩子 2018-11-07 11:50

个回答

  • MMD_野孩子
    2018-11-07 12:00:05

    找到原因了