LeetCode Recover Binary Search Tree
2014-08-25 00:32
393 查看
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?
confused what
先来个"A solution using O(n) space is pretty straight forward"的方法。如果递归调用栈不算额外空间的话,把先序遍历改一下即可,如果算的话...怎么办
来一个伪常数空间的:
第二轮:
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.
class Solution { private: vector<TreeNode*> nodes; public: void recoverTree(TreeNode *root) { nodes.clear(); dfs(root); // 1 5 3 4 2 6 7 // 1 3 2 4 // 3 2 int len = nodes.size(); if (len < 1) return; int pre = nodes[0]->val; int cur = 0; int first = -1; int second= -1; for (int i=1; i<len; i++) { cur = nodes[i]->val; if (cur < pre) { if (first < 0) { first = i-1; } else { second= i; } } pre = cur; } if (second < 0) second = first + 1; int t = nodes[first]->val; nodes[first]->val = nodes[second]->val; nodes[second]->val= t; } void dfs(TreeNode* root) { if (root == NULL) return; dfs(root->left); nodes.push_back(root); dfs(root->right); } };
先来个"A solution using O(n) space is pretty straight forward"的方法。如果递归调用栈不算额外空间的话,把先序遍历改一下即可,如果算的话...怎么办
来一个伪常数空间的:
/** * 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<TreeNode*> nodes; TreeNode* pre; TreeNode* first; TreeNode* second; public: void recoverTree(TreeNode *root) { nodes.clear(); pre = first = second = NULL; dfs(root); // case a. 1 5 3 4 2 6 7 // case b. 1 3 2 4 // case c. 3 2 // case d. 3 // case e. NULL if (second == NULL) return; // case (d,e) int t = first->val; first->val = second->val; second->val= t; } void dfs(TreeNode* root) { if (root == NULL) return; dfs(root->left); visit(root); dfs(root->right); } void visit(TreeNode* node) { if (pre == NULL) { pre = node; return; } if (node->val < pre->val) { if (first == NULL) { first = pre; second= node; // assume swap with node next to pre(case b,c) } else { second= node; // fix above assumption(case a) } } pre = node; } };
第二轮:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { private: TreeNode* first; TreeNode* second; TreeNode* last; public: void recoverTree(TreeNode* root) { last = first = second = NULL; dfs(root); swap(first->val, second->val); } void dfs(TreeNode* root) { if (root == NULL) { return; } dfs(root->left); if (last != NULL) { if (last->val > root->val) { if (first == NULL) { first = last; } second = root; } } last = root; dfs(root->right); } };
相关文章推荐
- LeetCode 99 Recover Binary Search Tree(Python详解及实现)
- 【LeetCode】 Recover Binary Search Tree BST 中序遍历
- 【LeetCode】Recover BinarySearch Tree
- [LeetCode] Recover Binary Search Tree
- leetcode Recover Binary Search Tree
- Recover Binary Search Tree [LeetCode]
- LeetCode – Refresh – Recover Binary Search Tree
- LeetCode:Recover Binary Search Tree
- 二叉树系列 - 二叉搜索树 - [LeetCode] 中序遍历中利用 pre节点避免额外空间。题:Recover Binary Search Tree,Validate Binary Search Tree
- 【Leetcode】Recover Binary Search Tree
- LeetCode OJ:Recover Binary Search Tree(恢复二叉搜索树)
- leetcode 099 —— Recover Binary Search Tree
- [LeetCode] 078: 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