How does this inorder traversal algorithm work?

后端 未结 3 542
眼角桃花
眼角桃花 2020-12-18 07:38

I don\'t have much experience with recursion, so I\'m having a hard time determining exactly how this algorithm works:

 public static void inorder(Node

        
3条回答
  •  被撕碎了的回忆
    2020-12-18 07:59

    Until you get used to recursion, it seems amazing that a little code can accomplish so much. In this case, you should try going through the algorithm by hand on an actual tree to see what happens.

    In the tree below from Wikipedia, you can see an in-order traversal that prints out A, B, C, D, E, F, G, H, I.

    1. It starts at F and calls inorder on the leftmost Node.
    2. It then calls inorder on B, and then calls inorder on A.
    3. A gets printed and then the Algorithm continues where it was in the previous call on B...

    (See more on my Tree Traversal Tutorial.)

    wiki tree

提交回复
热议问题