Welcome to read the reference, if there are errors or questions, please leave a message to correct, thank you
Recover Binary Search Tree
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 about how binary tree was serialized on OJ.
OJ ' s Binary Tree serialization:
The serialization of a binary tree follows a level order traversal, where ' # ' signifies a path terminator where no node ex Ists below.
Here's an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
Idea one: The middle sequence traversal print out, and then re-build after sorting, does not meet the requirements of O (n) space.
Thought two: 1, if you put the binary search tree node value with the middle sequence of the way to print out, then there is a position on the number is greater than the number adjacent to it, and then there is a position on the number less than the number adjacent to it. These two numbers are just next to each other for consideration.
2, so, we just need to find out that is greater than the number of a number node1, and then find that less than its previous number of Node2, and then exchange the value of the two will be OK. To achieve this, we need to save the previous node pre, and then find the Pre > cur anomalies (two total). At this point, if Node1 has been found, then we let node2 = cur; otherwise, we make Node1 = Pre.
3, Next we consider two numbers adjacent to the situation. Since the two numbers are adjacent, we can only find a pre > cur case and cannot assign a value to the Node2. But we notice that at this time cur is exactly what we are looking for node2. Back to the general situation, even if the cur is not the node2 we are looking for, we will be the second time after the pre > cur situation, we would still assign value to Node2. So, when we first met the Pre > cur, we assigned the pre and the cur to node1 and node2, and then, if we were to meet the pre > cur again, we would change node2 to the correct value and keep the value of Node1 unchanged. In this way, we can always find the right node1 and Node2.
Method One/** * Definition for binary tree * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right; *
treenode (int x): Val (x), left (null), right (NULL) {} *}; */class solution {TreeNode *node1; TreeNode *node2; TreeNode *pre;private: void Dfs (TreeNode *root) { if (root->left!=null) dfs (root->left); if (pre! = NULL) if ( pre->val > Root->val) if (node1==null) { node1 = pre; Node2 = root; } else node2 = root; Pre = root; if (root->right!=null) DFS (root->right); } Public: void Recovertree (TreeNode *root) { node1=node2=pre=null; DFS (root); Swap (Node1->val, node2->val);} ;
//method Two: Other people version class solution {Public:void recorvertreeutil (treenode* root, treenode*& Prev, treenode*& First, treenode*& Mid, treenode*& second) {if (root = NULL) return; Recorvertreeutil (Root->left, Prev, first, Mid, second); if (prev! = null && prev->val > Root->val) {if (first = = null) First = prev, mid = root; else second = root; } prev = root; Recorvertreeutil (Root->right, Prev, first, Mid, second); } void Recovertree (TreeNode *root) {//Start typing your C + + solution below//do not write int main () function treenode* first = NULL; treenode* mid = NULL; treenode* second = NULL; treenode* prev = NULL; Recorvertreeutil (Root, Prev, first, Mid, second); if (second! = NULL) Swap (first->val, second->val); Else swap (first->val, mid->val); }};
Leetcode_99_recover Binary Search Tree