first commit
This commit is contained in:
@@ -224,7 +224,31 @@ root->right = traversal(nums, maxValueIndex + 1, right);
|
||||
|
||||
|
||||
Java:
|
||||
```java
|
||||
class Solution {
|
||||
public TreeNode constructMaximumBinaryTree(int[] nums) {
|
||||
if (nums.length == 0) return null;
|
||||
return constructMaximumBinaryTree(nums,0,nums.length);
|
||||
}
|
||||
|
||||
private TreeNode constructMaximumBinaryTree(int[] nums, int begin, int end) {
|
||||
if (begin == end) return null;
|
||||
|
||||
int rootValue = nums[begin];
|
||||
int index = begin;
|
||||
for (int i = begin; i < end ; i++) {
|
||||
if (nums[i] > rootValue) {
|
||||
rootValue = nums[i];
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
TreeNode root = new TreeNode(rootValue);
|
||||
root.left = constructMaximumBinaryTree(nums, begin,index);
|
||||
root.right = constructMaximumBinaryTree(nums,index+1,end);
|
||||
return root;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
@@ -238,4 +262,4 @@ Go:
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
Reference in New Issue
Block a user