@Override
public Node findKey(int value) {
return this.findKey(value, root);
}
public Node findKey(int value,Node root) {
if(root == null){
return null;
}else if(root != null && root.value == value){
return root;
}else {
Node node1 = this.findKey(value, root.leftChild);
Node node2 = this.findKey(value, root.rightChild);
if(node1 != null && node1.value == value){
return node1;
}else if(node2 != null && node2.value == value){
return node2;
}else{
return null;
}
}
}
public Node findKey2(int value) {
Node node = null;
// 中序遍历左子树
if (root.leftChild != null) {
BinaryTree leftTree = new LinkedBinaryTree(root.leftChild);
node = leftTree.findKey(value);
}
if (node != null) {
return node;
} else {
// 判断根是否等于要查询值
if (root.value == value) {
return root;
} else {
// 中序遍历右子树
if (root.rightChild != null) {
BinaryTree rightTree = new LinkedBinaryTree(root.rightChild);
node = rightTree.findKey(value);
if (node != null) {
return node;
} else {
return null;
}
}
}
}
return null;
}