Recover the Swapped Keys

medium binary-search-tree trees stack 1000 ms · 256 MB

Recover the Swapped Keys

A binary search tree (BST) with n nodes originally had n distinct keys and satisfied the BST property: every node's key is strictly greater than every key in its left subtree and strictly less than every key in its right subtree.

Afterward, exactly two of the nodes had their keys swapped with each other by mistake. The shape of the tree — every left/right child pointer — is untouched; only two key values changed places.

You are given the corrupted tree. Find the two keys that were swapped, and figure out the key that each node should hold once the swap is undone.

It is guaranteed that the input really can be fixed by swapping back exactly one pair of node keys, and that this pair is unique.

Input

The first line contains two integers n and r (2 ≤ n ≤ 10^5, 1 ≤ r ≤ n) — the number of nodes and the index of the root.

Each of the next n lines describes one node. The i-th of these lines contains three integers key_i, left_i, right_i (-10^9 ≤ key_i ≤ 10^9, 0 ≤ left_i, right_i ≤ n) — node i's current key, and the indices of its left and right children, or 0 if a child doesn't exist. All n keys are distinct (even after the mistake, since it's a swap and not a duplication). The nodes and child pointers form a single tree rooted at r: every node other than r is the child of exactly one other node, and there are no cycles.

The tree may be as deep as n (a straight chain), so an algorithm that recurses once per node risks overflowing the call stack — traverse iteratively.

Output

Print two lines.

On the first line, print the two swapped keys, in increasing order.

On the second line, print n integers: the key that node 1, node 2, …, node n should hold after undoing the swap, in that order.

Notes

In the first example the tree (read by node index 1..5) is:

        3
       / \
      1   4
       \   \
        2   5

with keys [10, 30, 20, 40, 50] for nodes 1..5. Walking the tree in order (node 1, 2, 3, 4, 5) gives key sequence 10, 30, 20, 40, 50, which dips exactly once, from 30 down to 20 — the two swapped nodes are adjacent in sorted order. Undoing the swap restores 10, 20, 30, 40, 50.

In the second example the same shape has keys [50, 20, 30, 40, 10]. The in-order sequence now dips twice (50→20 and 40→10): the two swapped nodes are not adjacent in sorted order. The swapped keys are the first node of the first dip and the second node of the second dip.

Example 1

Input

5 3
10 0 2
30 0 0
20 1 4
40 0 5
50 0 0

Output

20 30
10 20 30 40 50

Example 2

Input

5 3
50 0 2
20 0 0
30 1 4
40 0 5
10 0 0

Output

10 50
10 20 30 40 50

Hints · reveal one at a time if you're stuck

Hint 1

In a correct BST, an in-order traversal lists the keys in increasing order. What does swapping two keys do to that sequence, and how many places can it go wrong?

Hint 2

Walk the tree in order without recursion, comparing each key with the previous one. The two swapped nodes are the ones at the places where the order breaks, and the first and last breaks decide which pair they are.

report an issue Prefer your own editor? sooks prep 74 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.