java中二叉树的一种实现方法
fill()方法用于填充高度级别内的所有空树节点。但是当我运行这段代码时,它抛出了NullPointerException,我不知道这里发生了什么java中二叉树的一种实现方法,java,Java,fill()方法用于填充高度级别内的所有空树节点。但是当我运行这段代码时,它抛出了NullPointerException,我不知道这里发生了什么 public void fill() { int height = height(overallRoot); overallRoot = fill(overallRoot, height); } //Fill all of the nodes within the height level private IntTreeNode f
public void fill() {
int height = height(overallRoot);
overallRoot = fill(overallRoot, height);
}
//Fill all of the nodes within the height level
private IntTreeNode fill(IntTreeNode root, int height) {
if (height == 0) { //if reaches the max height, don't add any node
return null;
} else if (root == null) { //if do not reach max height and root is null, add a series
//of new nodes until it reaches the max height
return new IntTreeNode(0, fill(root.left, height - 1), fill(root.right, height - 1));
} else {
root.left = fill(root.left, height - 1);
root.right = fill(root.right, height - 1);
}
return root;
}
//returns the height of a tree
private int height(IntTreeNode root) {
if (root == null) {
return 0;
} else {
return 1 + Math.max(height(root.left), height(root.right));
}
}
查看您的第一个
否则,如果。您首先确定(root==null)
,然后尝试检查它的左
和右
,尽管已经知道它是null
,因此没有左
或右
,但它正在寻求调试帮助,但需要更多信息。应更新问题,以包括所需行为、特定问题或错误,以及重现问题所需的最短代码。