当对象处于调试模式时,该对象不是null,而是通过方法调用时为空?
当我运行treenode代码时,编译器说由CountunivalSubtrees方法调用的treeNode为null。但是,当我运行调试模式时,它告诉我,我创建的treenode不是零。类Treenode的对象,foolbar具有其根,左分支和右分支。那么,为什么当名调用doombar时,将收到该方法的空对象呢?
package lc_250;
public class TreeNode {
int val;
TreeNode root;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
public void put(int val){
this.root = put(this.root, val);
}
private TreeNode put(TreeNode node_now, int val){
if (node_now == null) return new TreeNode(val);
else if (val < node_now.val) node_now.left = put(node_now.left, val);
else if (val > node_now.val) node_now.right = put(node_now.right, val);
else node_now.val = val;
return node_now;
}
public static void main(String[] args) {
TreeNode foolbar = new TreeNode(3);
foolbar.put(5);
foolbar.put(6);
foolbar.put(3);
foolbar.put(2);
foolbar.put(4);
foolbar.put(9);
foolbar.put(8);
foolbar.put(7);
cson250 foolclass = new cson250();
int num = foolclass.countUnivalSubtrees(foolbar);
}
}
-
package lc_250;
public class cson250 {
public int num = 0;
public int countUnivalSubtrees(TreeNode root) {
count(root);
return num;
}
public boolean count(TreeNode root){
if (root == null) System.out.println("bro Treenode is null");
boolean left = count(root.left);
boolean right = count(root.right);
if ( root.left == null && root.right == null){
num++;
return true;
}
if (left&&right){
if (root.left!=null && root.left.val != root.val){
return false;
}
if (root.right!= null && root.right.val != root.val){
return false;
}
num++;
return true;
}
return false;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在调试器中显示的观点与NPE发生的位置不同。这就是为什么您的手表字段不显示
root == null
的原因。在树的每一个叶子中,
root
将为null
,因此当您遇到第一片叶子时(应该是带有val“ 2”的节点,因为这是最小的列表中的值,然后将较小的值放在左节点上,然后在count
方法中选中左节点,您将首先执行,但您的代码继续执行
,并在那里执行
nullpointerexception
发生,因为root
当然仍然是null
,因此您无法访问root.left.left
。您的所有检查以防止汇总后发生递归电话,因此它们实际上并没有帮助停止访问
root
是null
。You are showing a different point in the debugger then where the NPE is happening. This is the reason why your watched fields don't show that
root == null
.In every leaf of your tree, the
root
will benull
, so when you encounter the first leaf (should be the node with val "2", since this is the smallest value in your list and you put the smaller values on the left nodes and you check the left nodes first in yourcount
method), you will first executebut then your code continues execution of
and there the
NullPointerException
occurs, becauseroot
of course still isnull
and so you cannot accessroot.left
.All your checks to prevent the recursive call happen after the call, so they don't really help to stop accessing a
root
that isnull
.我建议,如果您想在调试模式下将树节点视为null,请使您的代码看起来像这样,以便在root == null时放置调试点。
这样,您将在程序流中的某个时刻看到词根。
您可以在打印系统的线上放置一个调试点。
I would suggest, if you want to see your tree node as null in debug mode, make your code look like this, so as to put debug point when root == null.
This way you will see your root in null at some point in your program flow.
You can put a debug point at line where you are printing System.out.println("bro Treenode is null");