-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPartitionList.java
More file actions
38 lines (33 loc) · 951 Bytes
/
PartitionList.java
File metadata and controls
38 lines (33 loc) · 951 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package cc.dectinc.leetcode;
import cc.dectinc.leetcode.structs.TreeNode;
/**
* @author Dectinc (i@dectinc.cc)
* @date 15-04-01 23:01
*/
public class PartitionList {
TreeNode firstNode = null;
TreeNode secondNode = null;
TreeNode preNode = new TreeNode(Integer.MIN_VALUE);
public void recoverTree(TreeNode root) {
inorderTraversal(root);
int temp = firstNode.val;
firstNode.val = secondNode.val;
secondNode.val = temp;
}
public void inorderTraversal(TreeNode root) {
if (root == null) {
return;
}
inorderTraversal(root.left);
if (firstNode == null && preNode.val >= root.val) {
firstNode = preNode;
}
if (firstNode != null && preNode.val >= root.val) {
secondNode = root;
}
preNode = root;
inorderTraversal(root.right);
}
public static void main(String args[]) {
}
}