[LeetCode] Recover Binary Search Tree
2012-11-19 19:50
337 查看
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. Could you devise a constant space solution?
这题用O(n)的辅助空间比较好做,中序遍历后排个序O(nlogn)。但要不改变树的结构来完成就比较难了。
我只能想到一个把bst转为double link list后排序,再转为bst的方法,但转为bst就没法保证依然是原来bst的结构了。
空间O(1)
空间O(1)
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?
这题用O(n)的辅助空间比较好做,中序遍历后排个序O(nlogn)。但要不改变树的结构来完成就比较难了。
我只能想到一个把bst转为double link list后排序,再转为bst的方法,但转为bst就没法保证依然是原来bst的结构了。
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { private: vector<int> val; vector<TreeNode* > index; public: void traverse(TreeNode *node) { if (node == NULL) return; traverse(node->left); val.push_back(node->val); index.push_back(node); traverse(node->right); } void recoverTree(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function traverse(root); sort(val.begin(), val.end()); for(int i = 0; i < val.size(); i++) index[i]->val = val[i]; } };
空间O(1)
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void treeWalk(TreeNode* root, TreeNode*& prv, TreeNode*& first, TreeNode*& second) { if(root==NULL) return; treeWalk(root->left,prv,first,second); if((prv!=NULL)&&(prv->val>root->val)){ if(first==NULL) first=prv; second=root; } prv=root; treeWalk(root->right,prv,first,second); } void recoverTree(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function TreeNode* first=NULL; TreeNode* second=NULL; TreeNode* prv=NULL; treeWalk(root,prv,first,second); int tmp=first->val; first->val=second->val; second->val=tmp; } };
空间O(1)
class Solution { public: void treeWalk(TreeNode* root, TreeNode*& prv, TreeNode*& first, TreeNode*& second) { if(root==NULL) return; treeWalk(root->left,prv,first,second); if((prv!=NULL)&&(prv->val>root->val)){ if(first==NULL) first=prv; second=root; } prv=root; treeWalk(root->right,prv,first,second); } void recoverTree(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function TreeNode* first=NULL; TreeNode* second=NULL; TreeNode* prv=NULL; treeWalk(root,prv,first,second); int tmp=first->val; first->val=second->val; second->val=tmp; } };
相关文章推荐
- [LeetCode] Recover Binary Search Tree 解题报告
- [Leetcode] Recover binary search tree 恢复二叉搜索树
- LeetCode(99)Recover Binary Search Tree
- 【leetcode】Recover Binary Search Tree
- leetcode第一刷_Recover Binary Search Tree
- [LeetCode] Recover Binary Search Tree
- 【leetcode】Recover Binary Search Tree
- leetcode[99] Recover Binary Search Tree
- [Leetcode] Recover Binary Search Tree
- 【LeetCode】Recover Binary Search Tree 解题报告
- [leetcode]Recover Binary Search Tree
- leetcode - Recover Binary Search Tree
- [Leetcode] Recover Binary Search Tree
- 【leetcode】Recover Binary Search Tree
- [LeetCode]题解(python):099-Recover Binary Search Tree
- LeetCode Recover Binary Search Tree
- Leetcode-Recover Binary Search Tree
- LeetCode--Recover Binary Search Tree
- LeetCode | Recover Binary Search Tree(恢复二叉搜索树)
- 【Leetcode】Recover Binary Search Tree