Recover Binary Search Tree - LeetCode
2014-02-22 11:57
537 查看
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
read more on how binary tree is serialized on OJ.
Discuss
重点在中序遍历,注意这一步
以下是AC代码:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private TreeNode first;
private TreeNode second;
private TreeNode prev;
public void recoverTree(TreeNode root) {
if (root == null) {
return;
}
first = null;
second = null;
prev = null;
recoverTreeHelper(root);
switchValue(first, second);
}
private void switchValue(TreeNode node1, TreeNode node2) {
int val = node1.val;
node1.val = node2.val;
node2.val = val;
}
private void recoverTreeHelper(TreeNode root) {
if (root == null) {
return;
}
recoverTreeHelper(root.left);
if (prev != null && prev.val > root.val) { if (first == null) { first = prev; } second = root; }
prev = root;
recoverTreeHelper(root.right);
}
}
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.
Discuss
重点在中序遍历,注意这一步
if (prev != null && prev.val > root.val) { if (first == null) { first = prev; } second = root; }
以下是AC代码:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private TreeNode first;
private TreeNode second;
private TreeNode prev;
public void recoverTree(TreeNode root) {
if (root == null) {
return;
}
first = null;
second = null;
prev = null;
recoverTreeHelper(root);
switchValue(first, second);
}
private void switchValue(TreeNode node1, TreeNode node2) {
int val = node1.val;
node1.val = node2.val;
node2.val = val;
}
private void recoverTreeHelper(TreeNode root) {
if (root == null) {
return;
}
recoverTreeHelper(root.left);
if (prev != null && prev.val > root.val) { if (first == null) { first = prev; } second = root; }
prev = root;
recoverTreeHelper(root.right);
}
}
相关文章推荐
- leetcode~Recover Binary Search Tree
- leetcode hard模式专杀之99. Recover Binary Search Tree
- leetcode---recover-binary-search-tree---树
- [leetcode-99]Recover Binary Search Tree(java)
- Leetcode Recover Binary Search Tree
- LeetCode Recover Binary Search Tree
- LeetCode99—Recover Binary Search Tree
- [leetcode] #25 Recover Binary Search Tree
- Recover Binary Search Tree -- LeetCode
- [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] 中序遍历中利用 pre节点避免额外空间。题:Recover Binary Search Tree,Validate Binary Search Tree
- leetcode 124: Recover Binary Search Tree
- leetcode_c++:树: Recover Binary Search Tree(099)
- [LeetCode]Recover Binary Search Tree
- LeetCode---Recover Binary Search Tree
- Recover Binary Search Tree leetcode c++