public static void buildBinarySearchTree(SearchTreeNode currentNode,SearchTreeNode insertNode){
if (insertNode == null)
{
return;
}
if (currentNode.value>=insertNode.value){
if (currentNode.left==null){
currentNode.left = insertNode;
return;
}
else {
buildBinarySearchTree(currentNode.left,insertNode);
}
}
else if (currentNode.value<insertNode.value){
if (currentNode.right == null){
currentNode.right = insertNode;
return;
}
else {
buildBinarySearchTree(currentNode.right,insertNode);
}
}
}
}
class SearchTreeNode{
SearchTreeNode parent ;
SearchTreeNode left ;
SearchTreeNode right ;
int value;
SearchTreeNode(int value){
this.value = value;
parent = null;
right = null;
left = null;
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。