简体   繁体   English

如何计算只有一个孩子的二叉树中的节点数?

[英]how to count the number of nodes in a binary tree with only one child?

I have implemented the following function: 我已经实现了以下功能:

public int count(Node n) {
    if (n == null) {
        return 0;
    } else if (n.left == null && n.right != null) {
        return 1 + count(n.right);
    } else if (n.left != null && n.right == null) {
        return 1 + count(n.left);
    }
    return 0;
}

The problem is when I called it with: 问题是当我用以下命令调用它时:

System.out.println(tree.count(tree.root));

It only prints me the value of the root. 它仅显示根的值。 What am I doing wrong? 我究竟做错了什么?

Your code seems to have elements of both an instance method and a static method, which is a bit confusing. 您的代码似乎同时具有实例方法和静态方法的元素,这有点令人困惑。 Choose one, and be consistent in similar methods. 选择一个,并在类似方法中保持一致。 The simplest way is to use the bitwise xor ^ (returns true if exactly one of the two expressions are true ) 最简单的方法是使用按位xor ^ (如果两个表达式之一完全true则返回true

This is a static method. 这是一种static方法。 Called using Node.countNonBranchingNodes(tree) : 使用Node.countNonBranchingNodes(tree)调用:

public static int countNonBranchingNodes(Node n) {
    if (n == null) return 0;
    return (n.left != null ^ n.right != null ? 1 : 0) +
           countNonBranchingNodes(n.left) +
           countNonBranchingNodes(n.right);
}

If you want an instance method version, call this using tree.countNonBranchingNodes() : 如果要使用实例方法版本,请使用tree.countNonBranchingNodes()调用:

public int countNonBranchingNodes() {
    int count = left != null ^ right != null ? 1 : 0;
    if (left != null) count += left.countNonBranchingNodes();
    if (right != null) count += right.countNonBranchingNodes();
    return count;
}

In your code, you forget to handle the node which has both left and right child, so your code should like this: 在您的代码中,您忘记处理左右两个子节点,因此您的代码应如下所示:

public int count(Node n) {
    if (n == null) {
        return 0;
    } else if (n.left == null && n.right != null) {
        return 1 + count(n.right);
    } else if (n.left != null && n.right == null) {
        return 1 + count(n.left);
    } else {
        return count(n.left) + count(n.right);
    }
}

In your code, you have not handled the case when node is having both left and right children. 在您的代码中,您没有处理节点同时具有左右两个子节点的情况。 In that case we have to avoid that node to be counted but still we have to further proceed with counting in left and right subtrees. 在那种情况下,我们必须避免对该节点进行计数,但是仍然必须继续进行左右子树的计数。 But in your solution, If the node if having both the children, then you are simply returning 0, that's not correct. 但是在您的解决方案中,如果节点同时拥有两个孩子,那么您只是返回0,这是不正确的。

    public int countNodesWithExactlyOneChild(Node root){
        if(root == null) return 0;
        return (havingOneChild(root) ? 1 : 0) +
                   countNodesWithExactlyOneChild(root.left) + 
                   countNodesWithExactlyOneChild(root.right);

    }

    private boolean havingOneChild(Node node) {
        if(node != null && ((node.left == null && node.right != null) ||
            (node.left != null && node.right == null))) {
            return true;
        }
        return false;
    }

It seems your node will have 5 possibility: 看来您的节点将有5种可能性:

"left only", "right only", "both left and right", "neither left nor right" and "null". “仅左”,“仅右”,“左右”,“左右都不”和“空”。

public int count(Node n) {
    // null
    if (n == null) {
        return 0;
    // right only
    } else if (n.left == null && n.right != null) {
        return 1 + count(n.right);
    // left only
    } else if (n.left != null && n.right == null) {
        return 1 + count(n.left);
    // both left and right
    } else if (n.left != null && n.right != null) {
        return count(n.left) + count(n.right);
    // neither left nor right
    } else if (n.left == null && n.right == null) {
        return 1;
    // any else missing?
    } else {
        throw new RuntimeException();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM