CSC301: More Size: Student code [8/30] Previous pageContentsNext page

    public int size () {
        if (root == null) return 0;
        return size (root) - 1;
    }
    private static int size (Node x) {
        if (x == null) return 1;
        return size (x.left) + size (x.right);
    }

Cleaned up

Counts the number of nulls, rather than the number of nodes.

Correct, but not immediately obvious.

Previous pageContentsNext page