Turn a search tree into a sorted list · Compiler
Turn a search tree into a sorted list
Convert the given binary search tree into a doubly linked list with one node per key in increasing order. From the node with key x, walk k links in the given direction. The ends of the list have no links beyond them.
- Input
- Read n (1 <= n <= 100), then n rows of value left right. Rows give nodes in ID order from 1 to n. Each value is an integer in [-1000, 1000]. Each child link is a node ID or 0 for a missing child. For n > 0, the links form one binary tree rooted at ID 1. The tree is a BST with distinct keys. After the node rows, read x direction k. Key x is present, direction is PREV or NEXT, and 0 <= k <= 100.
- Output
- Print the key reached, or NONE if the walk passes either end. For k = 0, print x.
Compiler Explorer runs your code when you run or check it.
Loading your exercise draft…