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

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

Is it correct?

Can it be improved?

Previous pageContentsNext page