leetcode:Recover Binary Search Tree
来源:互联网 发布:sql怎么查平均数 编辑:程序博客网 时间:2024/05/16 10:22
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.
void help(TreeNode* root, TreeNode* &pre, TreeNode*&first, TreeNode*&second){if (root == NULL)return;help(root->left,pre, first, second);if (pre == NULL)pre = root;else{if (pre->val > root->val){if (first == NULL){first = pre;second = root;}else{second = root;return;}}pre = root;}help(root->right, pre, first, second);}void recoverTree(TreeNode *root) {TreeNode* pre = NULL;TreeNode* first = NULL, *second = NULL;if (root == NULL)return;help(root, pre, first, second);int tmp = first->val;first->val = second->val;second->val = tmp;}
0 0
- 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
- 进程的页表和页目录存储在内核空间还是用户空间?低端内存还是高端内存
- ubuntu下minicom和USB转串口(转)
- java使用layerpane设置背景图片
- JSON中存储Map的value将无法解析成相应的类型
- How to find a file in MS-DOS
- leetcode:Recover Binary Search Tree
- Animation and texture mapping
- new和delete重载
- 浅析JVM内存结构和6大区域
- MiniUI学习记录
- Unity3D学习之(知识点总结)
- 今天被cdn坑死
- 从WM2到mesh陆地
- Foundation框架第二弹:NSMutableString常用方法