Skip to content

Commit e4dfc7a

Browse files
add 897
1 parent 813dfd5 commit e4dfc7a

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Your ideas/fixes/algorithms are more than welcome!
3939
|922|[Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_922.java) | O(n) | O(1) | |Easy|
4040
|917|[Reverse Only Letters](https://leetcode.com/problems/reverse-only-letters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_917.java) | O(n) | O(n) | |Easy|
4141
|900|[Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_900.java) | O(n) | O(1) | |Easy|
42+
|897|[Increasing Order Search Tree](https://leetcode.com/problems/increasing-order-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_897.java) | O(n) | O(n) | |Easy| DFS, recursion
4243
|896|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_896.java) | O(n) | O(1) | |Easy|
4344
|884|[Uncommon Words from Two Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_884.java) | O(n) | O(k) | |Easy|
4445
|876|[Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_876.java) | O(n) | O(1) | |Easy|
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
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+
*/
48+
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+
}
55+
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+
}
68+
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+
}
80+
}
81+
}
82+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._897;
6+
import java.util.Arrays;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
public class _897Test {
11+
private static _897.Solution1 solution1;
12+
private static TreeNode root;
13+
private static TreeNode actual;
14+
15+
@Before
16+
public void setup() {
17+
solution1 = new _897.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
root = TreeUtils.constructBinaryTree(
23+
Arrays.asList(5, 3, 6, 2, 4, null, 8, 1, null, null, null, 7, 9));
24+
TreeUtils.printBinaryTree(root);
25+
actual = solution1.increasingBST(root);
26+
TreeUtils.printBinaryTree(actual);
27+
}
28+
}

0 commit comments

Comments
 (0)