[LeetCode] Recover Binary Search Tree 解题报告
来源:互联网 发布:大汉主义知乎 编辑:程序博客网 时间:2024/06/05 05:33
Note:
A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?
"{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.» Solve this problem
[解题报告]
O(n)空间的解法比较直观,中序遍历一遍以后,重新赋值一遍即可,这个解法可以面向n个元素错位的情况。但是对于O(1)空间的解法,最开始的想法是,可以考虑采用类似最大堆的调正过程的算法,但是这样又可能会破坏树的原有结构。暂未想出来解法。
只能给个O(n)空间的解法。
[Code]
1: void recoverTree(TreeNode *root) {
2: // Start typing your C/C++ solution below
3: // DO NOT write int main() function
4: vector<TreeNode*> list;
5: vector<int > vals;
6: InOrderTravel(root, list, vals);
7: sort(vals.begin(), vals.end());
8: for(int i =0; i< list.size(); i++)
9: {
10: list[i]->val = vals[i];
11: }
12: }
13: void InOrderTravel(TreeNode* node, vector<TreeNode*>& list, vector<int>& vals)
14: {
15: if(node == NULL) return;
16: InOrderTravel(node->left, list, vals);
17: list.push_back(node);
18: vals.push_back(node->val);
19: InOrderTravel(node->right, list, vals);
20: }
Updates: 3/16/2013
Searched in the web. Actually, there is a smart solution to travel the tree with two node.
The link is http://discuss.leetcode.com/questions/272/recover-binary-search-tree
1: void Solution::recoverTree(TreeNode *h) {
2: TreeNode *f1=NULL, *f2=NULL;
3: bool found = false;
4: TreeNode *pre, *par = 0; // previous AND parent
5: while(h) { // Morris Traversal
6: if(h->left == 0) {
7: if(par && par->val > h->val) { // inorder previous is: par
8: if(!found) {
9: f1 = par;
10: found = true;
11: }
12: f2 = h;
13: }
14: par = h;
15: h = h->right;
16: continue;
17: }
18: pre = h->left;
19: while(pre->right != 0 && pre->right != h)
20: pre = pre->right;
21: if(pre->right == 0) {
22: pre->right = h;
23: h = h->left;
24: } else {
25: pre->right = 0;
26: if(pre->val > h->val) { // inorder previous is: pre
27: if(!found) {
28: f1 = pre;
29: found =true;
30: }
31: f2 = h;
32: }
33: par = h;
34: h = h->right;
35: }
36: }
37: if(found)
38: swap(f1->val, f2->val);
39: }
Update: 3/21/2013 上一个解法不容易看清楚,添加分析。
O(1)的解法就是
Inorder traverse, keep the previous tree node,
Find first misplaced node by
if ( current.val < prev.val )
Node first = prev;
Find second by
if ( current.val < prev.val )
Node second = current;
After traversal, swap the values of first and second node. Only need two pointers, prev and current node. O(1) space.
但是这个解法的前提是Traverse Tree without Stack. 中序遍历如何才能不使用栈。这里就要引入一个概念, Threaded Binary Tree。So, we first create links to Inorder successor and print the data using these links, and finally revert the changes to restore original tree.
1. Initialize current as root
2. While current is not NULL
If current does not have left child
a) Print current’s data
b) Go to the right, i.e., current = current->right
Else
a) Make current as right child of the rightmost node in current's left subtree
b) Go to this left child, i.e., current = current->left
代码如下:
那么,基于这个双指针遍历,可以把错置节点的判断逻辑加进去,就可以完美的在O(1)空间内,完成树的重构。
[Code]
改动代码如红字所示。增加了一个pointer — parent来记录上一个访问节点。整个遍历过程中,使用(parent->val > current->val)来寻找违规节点,但是区别是,要获取第一次violation的parent和第二次violation的current,然后交换。
- 【LeetCode】Recover Binary Search Tree 解题报告
- [LeetCode] Recover Binary Search Tree 解题报告
- LeetCode(99) Recover Binary Search Tree解题报告
- [leetcode] 99. Recover Binary Search Tree 解题报告
- [Leetcode] 99. Recover Binary Search Tree 解题报告
- [leetcode]99. Recover Binary Search Tree@Java解题报告
- 【LeetCode】99.Recover Binary Search Tree(Hard)解题报告
- leetCode Recover Binary Search Tree解题分享
- Leetcode 99. Recover Binary Search Tree 恢复二叉搜索树 解题报告
- [LeetCode]Validate Binary Search Tree, 解题报告
- 【LeetCode】Validate Binary Search Tree 解题报告
- [LeetCode]Binary Search Tree Iterator,解题报告
- LeetCode-Binary Search Tree Iterator-解题报告
- [LeetCode] Validate Binary Search Tree 解题报告
- 【LeetCode】Validate Binary Search Tree 解题报告
- LeetCode: Recover Binary Search Tree
- LeetCode: Recover Binary Search Tree
- [Leetcode] Recover Binary Search Tree
- Java环境变量的配置
- [LeetCode] Permutations II 解题报告
- [LeetCode] Plus One 解题报告
- [LeetCode] Populating Next Right Pointers in Each Node 解题报告
- [LeetCode] Populating Next Right Pointers in Each Node II 解题报告
- [LeetCode] Recover Binary Search Tree 解题报告
- [LeetCode] Pow(x, n) 解题报告
- [LeetCode] Remove Duplicates from Sorted Array 解题报告
- ANSYS SpaceClaim 2015中文免费版
- [LeetCode] Remove Duplicates from Sorted Array II 解题报告
- [LeetCode] Remove Duplicates from Sorted List 解题报告
- [LeetCode] Remove Element 解题报告
- [LeetCode] Remove Nth Node From End of List 解题报告
- [LeetCode] Restore IP Addresses 解题报告