【发布时间】:2011-01-16 13:43:09
【问题描述】:
我正在搜索一棵树以查找传递的值。不幸的是,它不起作用。我开始用 print 调试它,奇怪的是它实际上找到了值,但跳过了 return 语句。
/**
* Returns the node with the passed value
*/
private TreeNode searchNodeBeingDeleted(Comparable c, TreeNode node)
{
if(node == null)
{
return null;
}
if(c.equals((Comparable)node.getValue()))
{
System.out.println("Here");
return node;
}
else
{
if(node.getLeft() != null)
{
System.out.println("left");
searchNodeBeingDeleted(c, node.getLeft());
}
if(node.getRight() != null)
{
System.out.println("right");
searchNodeBeingDeleted(c, node.getRight());
}
}
return null; //i think this gives me my null pointer at bottom
}
它打印出结果如下:
left
left
right
right
Here
right
left
right
left
right
Exception in thread "main" java.lang.NullPointerException
at Program_14.Driver.main(Driver.java:29)
我不知道这是否会有所帮助,但这是我的树:
L
/ \
D R
/ \ / \
A F M U
\ / \
B T V
感谢您的宝贵时间。
【问题讨论】:
标签: java algorithm recursion binary-tree