Trees and binary search trees · Exercise 28 of 100
Inorder traversal
Visit the entire left subtree, visit the node itself, then visit the entire right subtree. Apply this order at every node and skip empty links so that each node is visited once, between its two subtrees.
Try it yourself
Root 8 has left child 3 and right child 10. Node 3 has only a right child, 6, and node 6 has only a left child, 4. Which value is visited third in an inorder traversal?
One-step hint
Sketch the bend from 3 through its right child to 4. In inorder, finish each node's left subtree before recording the node itself.
Worked solution
- Go left from 8 to 3. Since 3 has no left child, visit 3 first, then enter its right subtree.
- At 6, visit its left child 4 before visiting 6 itself. That makes 4 second and 6 third.