Leetcode 99. Recover Binary Search Tree (Hard) (cpp)
来源:互联网 发布:domori巧克力知乎 编辑:程序博客网 时间:2024/06/06 00:34
Leetcode 99. Recover Binary Search Tree (Hard) (cpp)
Tag: Tree, Depth-first Search
Difficulty: Hard
/*99. Recover Binary Search Tree (Hard)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?*//*** 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 {public:void recoverTree(TreeNode* root) {TreeNode* first = NULL, *second = NULL, *pre = new TreeNode(INT_MIN);inOrder(root, first, second, pre);swap(first->val, second->val);return;}private:void inOrder(TreeNode* root, TreeNode*& first, TreeNode*& second, TreeNode*& pre) {if (root == NULL) {return;}inOrder(root->left, first, second, pre);if (pre->val >= root->val) {if (first == NULL) {first = pre;}if (first != NULL) {second = root;}}pre = root;inOrder(root->right, first, second, pre);}};
0 0
- Leetcode 99. Recover Binary Search Tree (Hard) (cpp)
- 【LeetCode】99.Recover Binary Search Tree(Hard)解题报告
- leetcode 日经贴,Cpp code -Recover Binary Search Tree
- leetcode 日经贴,Cpp code -Recover Binary Search Tree
- leetcode hard模式专杀之99. 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
- [leetcode] 99.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
- Leetcode 99. Recover Binary Search Tree
- LeetCode 99. Recover Binary Search Tree
- [LeetCode] 99. Recover Binary Search Tree
- leetcode-99. Recover Binary Search Tree
- [leetcode][67] Add Binary
- Binary Tree Right Side View
- 学习的文档位置
- TEC1401.Report开发技术总结 - 第七章 使用BI Publisher开发报表-创建一个简单的RTF模板(2/5)
- TEC1401.Report开发技术总结 - 第八章 使用BI Publisher开发报表-在EBS应用中部署BIP报表(3/5)
- Leetcode 99. Recover Binary Search Tree (Hard) (cpp)
- [leetcode][155] Min Stack
- Spring架构图
- TEC1401.Report开发技术总结 - 第九章 使用BI Publisher开发报表-使用BI Publisher创建RTF模板的语法(4/5)
- HDUoj 2141 Can you find it?(排序+二分)
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 272. Closest Binary Search Tree Value II (Hard) (cpp)
- TEC1401.Report开发技术总结 - 第十章 使用BI Publisher开发报表-其他注意事项(5/5)
- Notifycation使用入门