Skip to content

Commit 81887f5

Browse files
Recover Binary Search Tree
1 parent 45229bd commit 81887f5

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ My accepted leetcode solutions to some of the common interview problems.
324324
- [Split BST](problems/src/tree/SplitBST.java) (Medium)
325325
- [Closest Leaf in a Binary Tree](problems/src/tree/ClosestLeafInABinaryTree.java) (Medium)
326326
- [Maximum Width of Binary Tree](problems/src/tree/MaximumWidthOfBinaryTree.java) (Medium)
327+
- [Recover Binary Search Tree](problems/src/tree/RecoverBinarySearchTree.java) (Hard)
327328

328329
#### [Two Pointers](problems/src/two_pointers)
329330

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package tree;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 28/07/2018.
5+
* Two elements of a binary search tree (BST) are swapped by mistake.
6+
7+
Recover the tree without changing its structure.
8+
9+
Example 1:
10+
11+
Input: [1,3,null,null,2]
12+
13+
1
14+
/
15+
3
16+
\
17+
2
18+
19+
Output: [3,1,null,null,2]
20+
21+
3
22+
/
23+
1
24+
\
25+
2
26+
Example 2:
27+
28+
Input: [3,1,4,null,null,2]
29+
30+
3
31+
/ \
32+
1 4
33+
/
34+
2
35+
36+
Output: [2,1,4,null,null,3]
37+
38+
2
39+
/ \
40+
1 4
41+
/
42+
3
43+
Follow up:
44+
45+
A solution using O(n) space is pretty straight forward.
46+
Could you devise a constant space solution?
47+
48+
Solution: O(N) time and O(1) space. Step 1, perform a inorder traversal and mark left and right pointer at the node
49+
where violation of BST occurs. Step2, find the next node which is smaller or equal to right pointer node.
50+
Finally swap left and right node values.
51+
*/
52+
public class RecoverBinarySearchTree {
53+
private boolean violation;
54+
private TreeNode left, right, prev;
55+
56+
public static class TreeNode {
57+
int val;
58+
TreeNode left;
59+
TreeNode right;
60+
61+
TreeNode(int x) {
62+
val = x;
63+
}
64+
}
65+
66+
public static void main(String[] args) throws Exception{
67+
TreeNode root = new TreeNode(10);
68+
root.left = new TreeNode(1);
69+
root.left.left = new TreeNode(3);
70+
root.left.left.left = new TreeNode(5);
71+
new RecoverBinarySearchTree().recoverTree(root);
72+
}
73+
74+
public void recoverTree(TreeNode root) {
75+
inorder(root);
76+
if(left != null && right != null){
77+
int temp = left.val;
78+
left.val = right.val;
79+
right.val = temp;
80+
}
81+
}
82+
83+
private void inorder(TreeNode root){
84+
if(root != null){
85+
inorder(root.left);
86+
if(prev != null){
87+
if(!violation){
88+
if(prev.val > root.val){
89+
violation = true;
90+
left = prev;
91+
right = root;
92+
} else{
93+
prev = root;
94+
}
95+
} else{
96+
if(root.val <= right.val){
97+
right = root;
98+
}
99+
}
100+
} else {
101+
prev = root;
102+
}
103+
inorder(root.right);
104+
}
105+
}
106+
107+
}

0 commit comments

Comments
 (0)