0
我有一个二叉搜索树,我想有这样一个特定的值,以获得子树:Java的递归方法总是返回null
private Node getNode(Node root,Object c){
String data = String.valueOf(c);
if(root != null) {
getNode(root.left,c);
if(root.data.equals(data)){
System.out.println("found!!");
return root;
}
getNode(root.right,c);
}
return null;
}
System.out.println(getNode(root,c));
输出:
“发现!”
null
但它总是返回一个空值。我想知道为什么以及如何解决这个问题。谢谢!
使用调试器找到它。我的第一个猜测是,'root'为null – Jens
您对递归调用的返回值 –
不做任何处理,根目录不为空。 –