Elements of a binary search tree (BST) is swapped by mistake.
Recover the tree without changing its structure.
Note:
A solution using O (N) space is pretty straight forward. Could you devise a constant space solution?
Confused what "{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.
Subscribe to see which companies asked this question
I. Description of the topic
A binary tree was originally a two-fork search tree, but two of the nodes were replaced, requiring the original two-fork search tree to be restored.
second, the idea of solving problemsin order to traverse a binary tree, the values of the nodes that appear are sorted in ascending order, and if there are two nodes in the wrong position, it will definitely appear in descending order. sets a pre node pointer that records the front node of the current node in the sequence traversal, and if the current node is less than the pre-node value, it indicates that the order needs to be adjusted. If the point value occurs two times in descending order during the mid-sequence traversal, the first error node is the larger node with the first descending, and the second error node is the smaller node with the second descending. For example, the original search binary tree in the ordinal traversal of the node value in order {1,2,3,4,5}, if because the two node position is wrong and appears {1,5,3,4,2},The first descending is 5->3, so the first error node is 5, the second descending is 4->2, so the second error node is 2, and 5 and 2 can be recovered.
In order to traverse a binary tree, the values of the nodes that appear are sorted in ascending order, and if there are two nodes in the wrong position, it will definitely appear in descending order. Sets a pre node pointer that records the front node of the current node in the sequence traversal, and if the current node is less than the pre-node value, it indicates that the order needs to be adjusted. If the point value occurs two times in descending order during the mid-sequence traversal, the first error node is the larger node with the first descending, and the second error node is the smaller node with the second descending. For example, the original search binary tree in the ordinal traversal of the node value of {1,2,3,4,5}, if because two node position is wrong and the {1,5,3,4,2},//The first descending to 5->3, so the first error node is 5, the second descending is 4->2, So the second error node is 2, 5 and 2 can be restored. Class Solution {public:treenode* mistake1; treenode* Mistake2; treenode* Pre=null; void Recovertree (treenode* root) {recursive_traversal (root); if (mistake1! = NULL && Mistake2! = null) {Swap (mis Take1->val, Mistake2->val); }}//recursive middle sequence traversal binary tree void Recursive_traversal (treenode* root) {if (root = null) return; if (root->left! = null) {recursive _traversal (Root->left); } if (pre! = null && pre->val>root->val) {if (mistake1 = = null) {mistake1 = pre; mistake2 = root;} els e {mistake2 = root;}} Pre = root; if (root->right! = NULL) {recursive_traversal (root->right);}} };
Leetcode 99:recover Binary Search Tree