Skip to content

Commit 3be9ffb

Browse files
committed
fd
1 parent be532bb commit 3be9ffb

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
<br/><br/>
2121

2222
## **三、所有题目列表**
23-
|#|Title|Solution|Score|Backup|
24-
|---|----| ----- |----|---------|
23+
|#| Title |Solution|Score|Backup|
24+
|---|-------------| ----- |----|---------|
2525
|1|[Two Sum](https://leetcode.com/problems/two-sum/)|[Java](leetcode/solution/src/TwoSum.java)|100|
2626
|2|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/)| [Java](leetcode/solution/src/AddTwoNumber.java)|80|
2727
|3|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)| [Java](leetcode/solution/src/LongestSubstringWithoutRepeatingCharacters.java)||这个系列的所有题都要多做几遍|

leetcode/solution/src/MaximumBinaryTree.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
public class MaximumBinaryTree {
22

3+
/**
4+
* 复杂度平均O(nlgn),最差O(n^2)
5+
*/
36
public TreeNode constructMaximumBinaryTree(int[] nums, int start, int end) {
47
if (start > end) {
58
return null;

leetcode/src/Main.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,17 @@
44

55
public class Main {
66

7-
public TreeNode constructMaximumBinaryTree(int[] nums, int start, int end) {
8-
if (start > end) {
9-
return null;
7+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
8+
if (root == null || root == p || root == q) {
9+
return root;
1010
}
11-
int max = start;
12-
for (int i = start + 1; i <= end; i++) {
13-
if (nums[i] > nums[max]) {
14-
max = i;
15-
}
11+
TreeNode left = lowestCommonAncestor(root.left, p, q);
12+
TreeNode right = lowestCommonAncestor(root.right, p, q);
13+
if (left != null) {
14+
return right != null ? root : root.left;
15+
} else {
16+
return root.right;
1617
}
17-
TreeNode root = new TreeNode(nums[max]);
18-
root.left = constructMaximumBinaryTree(nums, start, max - 1);
19-
root.right = constructMaximumBinaryTree(nums, max + 1, end);
20-
return root;
21-
}
22-
23-
public TreeNode constructMaximumBinaryTree(int[] nums) {
24-
return constructMaximumBinaryTree(nums, 0, nums.length - 1);
2518
}
2619

2720
public static void main(String[] args) {

0 commit comments

Comments
 (0)