【发布时间】:2016-05-18 07:13:33
【问题描述】:
请任何人告诉我如何在二叉树中找到值是否存在? 我想查找值存在于二叉树的左节点还是右节点?
BinarySearchTree.prototype = {
//more code
contains: function(value){
var found = false,
current = this._root
//make sure there's a node to search
while(!found && current){
//if the value is less than the current node's, go left
if (value < current.value){
current = current.left;
//if the value is greater than the current node's, go right
} else if (value > current.value){
current = current.right;
//values are equal, found it!
} else {
found = true;
}
}
//only proceed if the node was found
return found;
}
}
【问题讨论】:
-
怎么不工作了?
-
i want to find value is present in left or right node of binary tree你的意思是在第一级吗? (因为树中有很多“右”和“左”。
标签: javascript html servlets