-1

I understand the concept of in order traversal for BST, but I am confused on how the recursion for it works. I am confused as to how the print statement and the recursion call with root.right as a parameter will ever be ran. I notice that inOder(root.left) will keep getting called to search to the lowest value in the BST, but when it reaches null then the if statement is unreachable, therefore we can't jump back to the right node. I just would like to know how we can possibly reach the bottom lines of code in the if statement.

public void inOrder(TreeNode root) { 
    if(root != null) { 
       inOrder(root.left); 
       System.out.printf("%d ",root.data); 
       inOrder(root.right); 
    } 
}

1 Answers1

0

It is important to understand the recursion stack. There are separate method calls stored in memory all the way down the left side. Printing starts only when calls for left stop. It prints the left child first, then the parent, then the right child starting at the bottom left.

cjkeilig
  • 101
  • 7