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

    public int size() {
        if (root == null) 
            return 0;
        return 1 + size (root);
    }

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

Is this correct?

Can it be improved?

Previous pageContentsNext page