Skip to content

Commit be532bb

Browse files
committed
fd
1 parent 4e44dc0 commit be532bb

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,5 +377,6 @@
377377
|637|[Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree/#/description)| [Java](leetcode/solution/src/AverageOfLevelsInBinaryTree.java)|100||
378378
|652|[Find Duplicate Subtrees](https://leetcode.com/problems/find-duplicate-subtrees/)| [Java](leetcode/solution/src/FindDuplicateSubtrees.java)|70|开始还没思路|
379379
|653|[Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)| [Java](leetcode/solution/src/TwoSumIV.java)|90||
380+
|654|[Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/description/)|[Java](leetcode/solution/src/MaximumBinaryTree.java)|100||
380381
|669|[Trim a Binary Search Tree](https://leetcode.com/problems/trim-a-binary-search-tree/)|[Java](leetcode/solution/src/TrimABinarySearchTree.java)|100||
381382
|684|[Redundant Connection](https://leetcode.com/problems/redundant-connection/)| [Java](leetcode/solution/src/RedundantConnection.java)|||
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class MaximumBinaryTree {
2+
3+
public TreeNode constructMaximumBinaryTree(int[] nums, int start, int end) {
4+
if (start > end) {
5+
return null;
6+
}
7+
int max = start;
8+
for (int i = start + 1; i <= end; i++) {
9+
if (nums[i] > nums[max]) {
10+
max = i;
11+
}
12+
}
13+
TreeNode root = new TreeNode(nums[max]);
14+
root.left = constructMaximumBinaryTree(nums, start, max - 1);
15+
root.right = constructMaximumBinaryTree(nums, max + 1, end);
16+
return root;
17+
}
18+
19+
public TreeNode constructMaximumBinaryTree(int[] nums) {
20+
return constructMaximumBinaryTree(nums, 0, nums.length - 1);
21+
}
22+
}

leetcode/src/Main.java

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

55
public class Main {
66

7-
public int countNodes(TreeNode root) {
8-
if (root == null) {
9-
return 0;
7+
public TreeNode constructMaximumBinaryTree(int[] nums, int start, int end) {
8+
if (start > end) {
9+
return null;
1010
}
11-
int left = 0, right = 0;
12-
for (TreeNode node = root; node != null; node = node.left, left++);
13-
for (TreeNode node = root; node != null; node = node.right, right++);
14-
if (left == right) {
15-
return (1 << left) - 1;
11+
int max = start;
12+
for (int i = start + 1; i <= end; i++) {
13+
if (nums[i] > nums[max]) {
14+
max = i;
15+
}
1616
}
17-
return countNodes(root.left) + countNodes(root.right) + 1;
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);
1825
}
1926

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

0 commit comments

Comments
 (0)