CSC300: Backward recursion, nullable argument [7/11] Previous pageContentsNext page

11
12
13
14
15
16
17
18
19
  public int numFives () {
    return numFivesH (first);
  }
  private static int numFivesH (Node x) {
    if (x == null) return 0;
    int result = numFivesH (x.next);
    if (x.item == 5) result = result + 1;
    return result;
  }

For [5,11,5,5], the call tree is

call@3 ([5,11,5,5])
  call@4 ([11,5,5])
    call@5  ([5,5])
      call@6  ([5])
        call@7 ([])
        retn@7 ([]) : 0
      retn@6  ([5]) : 1
    retn@5  ([5,5]) : 2
  retn@4 ([11,5,5]) : 2
retn@3 ([5,11,5,5]) : 3

Previous pageContentsNext page