The tests write a tree as a level-order list (row by row, left to right) with None for a missing child: [3, 9, 20, None, None, 15, 7] is a root 3 with children 9 and 20, where 20 has children 15 and 7. All trees in this topic that are called BSTs have different values.
Exactly two nodes of a BST had their values swapped by mistake. Put the values back so that the tree is a valid BST again, without changing the shape, and return the root.
For [3, 1, 4, None, None, 2] the answer is [2, 1, 4, None, None, 3].
Example 1
Input:
recover_tree(root) # root = [1, 3, None, None, 2]
Output:
[3, 1, None, None, 2]
Example 2
Input:
recover_tree(root) # root = [3, 1, 4, None, None, 2]
Output:
[2, 1, 4, None, None, 3]
2 <= number of nodes <= 100000No results yet
Run your code to check it against the examples, or submit to run every test.