Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中二叉树的一种实现方法_Java - Fatal编程技术网

java中二叉树的一种实现方法

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

fill()方法用于填充高度级别内的所有空树节点。但是当我运行这段代码时,它抛出了NullPointerException,我不知道这里发生了什么

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
,因此没有
,但它正在寻求调试帮助,但需要更多信息。应更新问题,以包括所需行为、特定问题或错误,以及重现问题所需的最短代码。