




Sponsored
Sponsored
This approach uses an additional list to store the nodes during an in-order traversal of the BST. The in-order traversal yields the values of the BST in a sorted manner for a correct BST configuration. If two nodes have been swapped, this sorted order will be violated in exactly two places. By storing all nodes during traversal and comparing them, we can identify the two swapped nodes. Once identified, we swap their values to correct the tree.
This method requires O(n) space to store the nodes' values where n is the number of nodes in the tree.
Time Complexity: O(n) since we are conducting an in-order traversal of the tree.
Space Complexity: O(n) as we store nodes in a list for comparison.
This Python solution performs an in-order traversal of the BST and stores the nodes in a list. It then iterates through this list to find the two nodes that are out of order, i.e., the swapped nodes, by checking where the current node value is less than the previous node value. Finally, it swaps the values of these nodes to correct the BST.
This approach leverages Morris Traversal to achieve O(1) space complexity, without needing an auxiliary stack or recursion. Morris Traversal utilizes the tree structure itself to keep track of nodes and ensures the tree is reset to its original configuration after traversal. To correct the BST, we detect swapped nodes by identifying violation of BST properties during traversal, and then we swap these nodes to achieve a valid BST.
Time Complexity: O(n).
Space Complexity: O(1).
1public class Solution {
2    public void RecoverTree(TreeNode root) {
3        TreeNode first = null, second = null, prev = null;
4        TreeNode current = root;
5        while (current != null) {
6            if (current.left == null) {
7                if (prev != null && current.val < prev.val) {
8                    second = current;
9                    if (first == null) first = prev;
10                }
11                prev = current;
12                current = current.right;
13            } else {
14                TreeNode pre = current.left;
15                while (pre.right != null && pre.right != current) pre = pre.right;
16                if (pre.right == null) {
17                    pre.right = current;
18                    current = current.left;
19                } else {
20                    pre.right = null;
21                    if (prev != null && current.val < prev.val) {
22                        second = current;
23                        if (first == null) first = prev;
24                    }
25                    prev = current;
26                    current = current.right;
27                }
28            }
29        }
30        int temp = first.val;
31        first.val = second.val;
32        second.val = temp;
33    }
34}This C# solution follows the Morris Traversal strategy, minimizing space usage by recompensing tree structure for the necessity of stack memory. It directly handles tree sequencing and discovers anomalies during traversal.