We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents b3d865b + e51cab0 commit b4c62f9Copy full SHA for b4c62f9
LeetCode/99. Recover Binary Search Tree/Solution.java
@@ -0,0 +1,28 @@
1
+class Solution {
2
+ TreeNode first=null;
3
+ TreeNode second=null;
4
+ TreeNode prev=new TreeNode(Integer.MIN_VALUE);
5
+ public void recoverTree(TreeNode root) {
6
+ inorder(root);
7
+
8
+ // swap values
9
+ int temp=first.val;
10
+ first.val=second.val;
11
+ second.val=temp;
12
+ }
13
+ private void inorder(TreeNode root){
14
+ if(root==null) return;
15
+ inorder(root.left);
16
17
+ //mark first node
18
+ if(first==null && prev.val>root.val)
19
+ first=prev;
20
21
+ // mark second node
22
+ if(first!=null && prev.val>root.val)
23
+ second=root;
24
25
+ prev=root;
26
+ inorder(root.right);
27
28
+}
0 commit comments