Order the steps
Put the explicit-stack in-order walk in order.
- Push the current node and move to its left child until you reach None
- Count it; if this is the k-th node, return its value
- Pop the top node from the stack
- Move to the popped node's right child and repeat
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A C B D
- Push the current node and move to its left child until you reach None The smallest value left is at the bottom of the leftmost path.
- Pop the top node from the stack It is the next value in sorted order.
- Count it; if this is the k-th node, return its value Stopping here avoids walking the rest of the tree.
- Move to the popped node's right child and repeat Values just above the popped one live in its right subtree.
Steps come from running the Python solution.
Text version
inorder:
Tree:
- 8
- 3 (left)
- 1 (left)
- 6 (right)
- 4 (left)
- 7 (right)
- 10 (right)
- 14 (right)
- 3 (left)
- Visit 1
- Visit 3
- Visit 4
- Visit 6
- Visit 7
- Visit 8
- Visit 10
- Visit 14
Result: 4. Order: 1, 3, 4, 6, 7, 8, 10, 14
preorder:
Tree:
- 8
- 3 (left)
- 1 (left)
- 6 (right)
- 4 (left)
- 7 (right)
- 10 (right)
- 14 (right)
- 3 (left)
- Visit 8
- Visit 3
- Visit 1
- Visit 6
- Visit 4
- Visit 7
- Visit 10
- Visit 14
Result: [8, 3, 1, 6, 4, 7, 10, 14]. Order: 8, 3, 1, 6, 4, 7, 10, 14