LeetCode 99: Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
Note:
A solution using O (n) space is pretty straight forward. cocould 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
A binary tree is originally a binary search tree, but two of the nodes have changed their locations and the original binary search tree needs to be restored.
II. In the solution, the binary tree is traversed sequentially, and the values of the nodes will be sorted in ascending order. If there are two nodes in wrong positions, then there will certainly be a descending order. Set a pre-node pointer to record the previous nodes in sequence of the current node. If the current node is smaller than the pre-node value, the order needs to be adjusted. If there are two descending orders for the sequential node value in the middle order, the first wrong node is the larger node in the first descending order, the second error node is a smaller node in the second descending order. For example, the node values of the original search binary tree in the middle-order traversal are {1, 2, 3, 4, 5} in sequence. If the two nodes are incorrect, {, 3, 4, 2} appears }, the first descending order is 5-> 3, so the first error node is 5, the second descending order is 4-> 2, so the second error node is 2, change 5 and 2 to restore.
// The binary tree is traversed in ascending order, and the values of the nodes will be sorted in ascending order. If there are two nodes with incorrect positions, the values will certainly be sorted in descending order. // Set a pre-node pointer to record the previous nodes in sequence in the current node. If the current node is smaller than the pre-node value, the order needs to be adjusted. // If there are two descending orders for the sequential node value in the middle order, the first wrong node is the larger node in the first descending order, the second error node is a smaller node in the second descending order. // For example, the node values of the original search binary tree in the central traversal are {1, 2, 3, 4, 5} in sequence. If the locations of the two nodes are incorrect, {1, 5, 3, 4, 2}, // The first descending order is 5-> 3, so the first error node is 5, the second descending order is 4-> 2, so the second error node is 2, change 5 and 2 to restore. Class Solution {public: TreeNode * mistake1; TreeNode * mistake2; TreeNode * pre = NULL; void recoverTree (TreeNode * root) {recursive_traversal (root); if (mistake1! = NULL & mistake2! = NULL) {swap (mistake1-> val, mistake2-> val) ;}// recursively traverse 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;} else {mistake2 = root ;}} pre = root; if (root-> right! = NULL) {recursive_traversal (root-> right );}}};