leetcode Recover Binary Search Tree
来源:互联网 发布:球球大作战补领号软件 编辑:程序博客网 时间:2024/05/14 05:28
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 "{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.
Give an inorder traversal for the binary tree.
Find the first node res1, which is larger than its successor.
Find the first node p, which is larger than res1->val. So the predecessor of p is the second index we wanna find.
However, in the recursive program, there are some mistake, here is wrong code:
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution { public: TreeNode *last = NULL, *res1 = NULL, *res2 = NULL; void inorder(TreeNode* root) { if (root == NULL) return; if (res1 && res2) return; inorder(root->left); if (last != NULL && res1 == NULL && last->val > root->val) res1 = last; else if (last != NULL && res1 != NULL && root->val > res1->val) { res2 = last; swap(res1->val, last->val); return; } last = root; inorder(root->right); } void recoverTree(TreeNode *root) { // Note: The Solution object is instantiated only once and is reused by each test case. last = res1 = res2 = NULL; inorder(root); if (res2 == NULL) swap(res1->val, last->val); //if (res1 && res2) // swap(res1->val, res2->val); }};
The wrong case is :Input:{10,5,15,0,8,13,20,2,-5,6,9,12,14,18,25}Output:{10,5,15,0,8,13,20,2,-5,6,9,12,14,18,25}Expected:{10,5,15,0,8,13,20,-5,2,6,9,12,14,18,25}
The reason is that the branch else if is executed more than once although returning. Take the WA case for example, after visiting 5, 8 is returned. But 10 is visited afterwards: TreeNode* last hasn't been updated, so swap the second time. The right code is:
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution { public: TreeNode *last = NULL, *res1 = NULL, *res2 = NULL; void inorder(TreeNode* root) { if (root == NULL) return; if (res1 && res2) return; inorder(root->left); if (last != NULL && res1 == NULL && last->val > root->val) res1 = last; else if (last != NULL && res1 != NULL && res2 ==NULL && root->val > res1->val) { res2 = last; swap(res1->val, last->val); return; } last = root; inorder(root->right); } void recoverTree(TreeNode *root) { // Note: The Solution object is instantiated only once and is reused by each test case. last = res1 = res2 = NULL; inorder(root); if (res2 == NULL) swap(res1->val, last->val); //if (res1 && res2) // swap(res1->val, res2->val); }};
- 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 -- 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] Recover Binary Search Tree
- [LeetCode] Recover Binary Search Tree
- 【Leetcode】Recover Binary Search Tree
- Leetcode Recover Binary Search Tree
- LeetCode | Recover Binary Search Tree
- android来电时开始查询并将查询的归属地显示到主界面上实现
- 黑马程序员_高薪技术_9
- Ideas for "Snap to grid"
- HDLC协议帧格式
- Java通用工具类之按对象属性排序工具类
- leetcode Recover Binary Search Tree
- 关于java调用webservice参数传递为空问题
- HTTP请求(GET与POST区别)和响应
- Eclipse中和编辑相关的快捷键
- 《推荐系统实践》阅读笔记三 LFM模型、图模型、slop one和SVD算法
- KMP算法的详细解释及实现
- wikioi 1219 骑士游历
- 删除重复记录的SQL语句
- Oracle 查询并删除重复记录的SQL语句