There's an implicit return
at the end of your void
method:
public static void preorder(Node root) {
if(root == null) return;
root.printValue();
preorder(root.getLeft());
preorder(root.getRight());
return;
}
You always return to the method that called you. So, if the parent's method call makes another call for the child, then when the child's method call returns, it returns to the parent's. Right?
Hope that makes sense. Like Kon said, you should just run through the algorithm on paper. Or, better yet, if you know how to use a debugger, you can just step through your code in the debugger and see how it works.