Skip to content

Commit 99e8e95

Browse files
committed
fd
1 parent f8d4d61 commit 99e8e95

File tree

2 files changed

+21
-45
lines changed

2 files changed

+21
-45
lines changed

leetcode/solution/src/TrimABinarySearchTree.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,14 @@ public class TrimABinarySearchTree {
33
public TreeNode trimBST(TreeNode root, int L, int R) {
44
if (root == null) {
55
return root;
6-
}
7-
8-
if (root.val > R) {
9-
root.right = null;
6+
} else if (root.val > R) {
107
return trimBST(root.left, L, R);
11-
}
12-
13-
if (root.val < L) {
14-
root.left = null;
8+
} else if (root.val < L) {
159
return trimBST(root.right, L, R);
10+
} else {
11+
root.left = trimBST(root.left, L, R);
12+
root.right = trimBST(root.right, L, R);
13+
return root;
1614
}
17-
18-
root.left = trimBST(root.left, L, R);
19-
root.right = trimBST(root.right, L, R);
20-
return root;
2115
}
2216
}

leetcode/src/Main.java

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,28 @@ public class Main {
66

77
public static class Solution {
88

9-
public int maxDistToClosest(int[] seats) {
10-
int max = 0;
11-
12-
int[] zone = new int[2];
13-
for (int index = 0; index < seats.length; ) {
14-
Arrays.fill(zone, -1);
15-
index = nextFree(seats, index, zone);
16-
if (zone[0] == 0 || zone[1] == seats.length - 1) {
17-
max = Math.max(max, zone[1] - zone[0] + 1);
18-
} else {
19-
max = Math.max(max, (zone[1] - zone[0] + 2) / 2);
20-
}
21-
}
22-
23-
return max;
9+
public TreeNode constructMaximumBinaryTree(int[] nums) {
10+
return helper(nums, 0, nums.length - 1);
2411
}
2512

26-
private int nextFree(int[] seats, int start, int[] zone) {
27-
boolean enter = false;
28-
for (int i = start, j = 0; i <= seats.length; i++) {
29-
if (i < seats.length && seats[i] == 0) {
30-
if (!enter) {
31-
enter = true;
32-
j = i;
33-
}
34-
} else {
35-
if (enter) {
36-
zone[0] = j;
37-
zone[1] = i - 1;
38-
return i + 1;
39-
}
13+
private TreeNode helper(int[] nums, int start, int end) {
14+
if (start > end) {
15+
return null;
16+
}
17+
int index, max = start;
18+
for (index = start + 1; index <= end; index++) {
19+
if (nums[index] > nums[max]) {
20+
max = index;
4021
}
4122
}
42-
return seats.length;
23+
TreeNode root = new TreeNode(nums[max]);
24+
root.left = helper(nums, start, max - 1);
25+
root.right = helper(nums, max + 1, end);
26+
return root;
4327
}
4428
}
4529

4630
public static void main(String[] args) {
47-
Solution solution = new Solution();
48-
int s = solution.maxDistToClosest(new int[]{1,0,0,0,1,0,1});
49-
System.out.println(s);
31+
5032
}
5133
}

0 commit comments

Comments
 (0)