Skip to content

Commit 6c39b9c

Browse files
add 1382
1 parent 618dab7 commit 6c39b9c

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11-
|1381|[Design a Stack With Increment Operation](https://leetcode.com/problems/design-a-stack-with-increment-operation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1381.java) | |Easy|Stack, Design|
11+
|1382|[Design a Stack With Increment Operation](https://leetcode.com/problems/balance-a-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1382.java) | |Medium|Binary Search Tree|
12+
|1381|[Design a Stack With Increment Operation](https://leetcode.com/problems/design-a-stack-with-increment-operation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1381.java) | |Medium|Stack, Design|
1213
|1380|[Lucky Numbers in a Matrix](https://leetcode.com/problems/lucky-numbers-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1380.java) | |Easy|Array|
1314
|1379|[Find a Corresponding Node of a Binary Tree in a Clone of That Tree](https://leetcode.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1379.java) | |Medium|Tree|
1415
|1377|[Frog Position After T Seconds](https://leetcode.com/problems/frog-position-after-t-seconds/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1377.java) | |Hard|DFS, BFS|
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
* 1382. Balance a Binary Search Tree
10+
*
11+
* Given a binary search tree, return a balanced binary search tree with the same node values.
12+
* A binary search tree is balanced if and only if the depth of the two subtrees of every node never differ by more than 1.
13+
* If there is more than one answer, return any of them.
14+
*
15+
* Example 1:
16+
* Input: root = [1,null,2,null,3,null,4,null,null]
17+
* Output: [2,1,3,null,null,null,4]
18+
* Explanation: This is not the only correct answer, [3,1,4,null,2,null,null] is also correct.
19+
*
20+
* Constraints:
21+
* The number of nodes in the tree is between 1 and 10^4.
22+
* The tree nodes will have distinct values between 1 and 10^5.
23+
* */
24+
public class _1382 {
25+
public static class Solution1 {
26+
public TreeNode balanceBST(TreeNode root) {
27+
List<Integer> inorder = inorder(root, new ArrayList<>());
28+
return dfs(inorder, 0, inorder.size() - 1);
29+
}
30+
31+
private List<Integer> inorder(TreeNode root, List<Integer> list) {
32+
if (root == null) {
33+
return list;
34+
}
35+
inorder(root.left, list);
36+
list.add(root.val);
37+
return inorder(root.right, list);
38+
}
39+
40+
private TreeNode dfs(List<Integer> nums, int start, int end) {
41+
if (end < start) {
42+
return null;
43+
}
44+
int mid = (start + end) / 2;
45+
TreeNode root = new TreeNode(nums.get(mid));
46+
root.left = dfs(nums, start, mid - 1);
47+
root.right = dfs(nums, mid + 1, end);
48+
return root;
49+
}
50+
}
51+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._1382;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import java.util.Arrays;
10+
11+
public class _1382Test {
12+
private static _1382.Solution1 solution1;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _1382.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, null, 2, null, 3, null, 4, null, null));
22+
TreeUtils.printBinaryTree(root);
23+
TreeUtils.printBinaryTree(solution1.balanceBST(root));
24+
}
25+
26+
}

0 commit comments

Comments
 (0)