Executions of height
follow the tree shape in order.
height
is called first on Root
and then recursively on Root.left
in line 4 and then again recursively on Root.left.left
, which is null
. At this point the last execution of height
returns -1
and terminates. The remaining last execution assigns this to left
and prints it.
This same execution nex calls height
on Root.left.right
. This is also null
, so -1
is returned and printed, after which the if statement computes -1 + 1 == 0
and returns this.
We are now back to the call on Root.left
, and the returned value 0
is assigned to left
and is printed, whereupon the same execution calls height
again and again on down to Root.right.left
, which is null
, so again -1
is printed.
You should see the pattern at this point.