We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent e335c21 commit 5acb0e8Copy full SHA for 5acb0e8
solution/0099.Recover Binary Search Tree/Solution.java
@@ -0,0 +1,19 @@
1
+class Solution {
2
+ private TreeNode first,second,pre;
3
+ public void recoverTree(TreeNode root) {
4
+ traverse(root);
5
+ int temp = first.val;
6
+ first.val = second.val;
7
+ second.val = temp;
8
+ }
9
+ private void traverse(TreeNode root) {
10
+ if (root == null) return;
11
+ traverse(root.left);
12
+ if (pre != null) {
13
+ if (first == null && pre.val > root.val) first = pre;
14
+ if (first != null && pre.val > root.val) second = root;
15
16
+ pre = root;
17
+ traverse(root.right);
18
19
+}
0 commit comments