Solution Review: nth Pre-Order

Let’s go through the detailed solution review of the challenge given in the previous lesson.

Solution

We keep track of our index in a counter. Because we want to print the node that is at the nthn^​{th} index in pre-order traversal, we’ll increment the counter as soon as we find a non-empty node. When the counter becomes equal to the index, we’ll print the value and return the nthn^{​th} pre-order index node.

Solution code

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.