Skip to content

Commit fcf1206

Browse files
refactor 897
1 parent 593ee91 commit fcf1206

File tree

1 file changed

+31
-71
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+31
-71
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,42 @@
11
package com.fishercoder.solutions;
22

33
import com.fishercoder.common.classes.TreeNode;
4+
45
import java.util.ArrayList;
56
import java.util.List;
67

7-
/**
8-
* 897. Increasing Order Search Tree
9-
*
10-
* Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child.
11-
*
12-
* Example 1:
13-
* Input: [5,3,6,2,4,null,8,1,null,null,null,7,9]
14-
*
15-
* 5
16-
* / \
17-
* 3 6
18-
* / \ \
19-
* 2 4 8
20-
* / / \
21-
* 1 7 9
22-
*
23-
* Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
24-
*
25-
* 1
26-
* \
27-
* 2
28-
* \
29-
* 3
30-
* \
31-
* 4
32-
* \
33-
* 5
34-
* \
35-
* 6
36-
* \
37-
* 7
38-
* \
39-
* 8
40-
* \
41-
* 9
42-
* Note:
43-
*
44-
* The number of nodes in the given tree will be between 1 and 100.
45-
* Each node will have a unique integer value from 0 to 1000.
46-
*
47-
*/
488
public class _897 {
49-
public static class Solution1 {
50-
public TreeNode increasingBST(TreeNode root) {
51-
List<Integer> inorderList = new ArrayList<>();
52-
inorderTraversal(root, inorderList);
53-
return constructTree(inorderList);
54-
}
9+
public static class Solution1 {
10+
public TreeNode increasingBST(TreeNode root) {
11+
List<Integer> inorderList = new ArrayList<>();
12+
inorderTraversal(root, inorderList);
13+
return constructTree(inorderList);
14+
}
5515

56-
private TreeNode constructTree(List<Integer> inorderList) {
57-
if (inorderList.isEmpty() || inorderList.size() == 0) {
58-
return null;
59-
}
60-
TreeNode root = new TreeNode(inorderList.get(0));
61-
TreeNode tmp = root;
62-
for (int i = 1; i < inorderList.size(); i++) {
63-
tmp.right = new TreeNode(inorderList.get(i));
64-
tmp = tmp.right;
65-
}
66-
return root;
67-
}
16+
private TreeNode constructTree(List<Integer> inorderList) {
17+
if (inorderList.isEmpty() || inorderList.size() == 0) {
18+
return null;
19+
}
20+
TreeNode root = new TreeNode(inorderList.get(0));
21+
TreeNode tmp = root;
22+
for (int i = 1; i < inorderList.size(); i++) {
23+
tmp.right = new TreeNode(inorderList.get(i));
24+
tmp = tmp.right;
25+
}
26+
return root;
27+
}
6828

69-
private void inorderTraversal(TreeNode root, List<Integer> inorderList) {
70-
if (root == null) {
71-
return;
72-
}
73-
if (root.left != null) {
74-
inorderTraversal(root.left, inorderList);
75-
}
76-
inorderList.add(root.val);
77-
if (root.right != null) {
78-
inorderTraversal(root.right, inorderList);
79-
}
29+
private void inorderTraversal(TreeNode root, List<Integer> inorderList) {
30+
if (root == null) {
31+
return;
32+
}
33+
if (root.left != null) {
34+
inorderTraversal(root.left, inorderList);
35+
}
36+
inorderList.add(root.val);
37+
if (root.right != null) {
38+
inorderTraversal(root.right, inorderList);
39+
}
40+
}
8041
}
81-
}
8242
}

0 commit comments

Comments
 (0)