Skip to content

Commit 46a9794

Browse files
committed
solve problem Maximum Binary Tree
1 parent 6017e4c commit 46a9794

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ All solutions will be accepted!
235235
|60|[Permutation Sequence](https://leetcode-cn.com/problems/permutation-sequence/description/)|[java/py/js](./algorithms/PermutationSequence)|Medium|
236236
|106|[Construct Binary Tree From Inorder And Postorder Traversal](https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/)|[java/py/js](./algorithms/ConstructBinaryTreeFromInorderAndPostorderTraversal)|Medium|
237237
|105|[Construct Binary Tree From Preorder And Inorder Traversal](https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/)|[java/py/js](./algorithms/ConstructBinaryTreeFromPreorderAndInorderTraversal)|Medium|
238+
|654|[Maximum Binary Tree](https://leetcode-cn.com/problems/maximum-binary-tree/description/)|[java/py/js](./algorithms/MaximumBinaryTree)|Medium|
238239

239240
# Database
240241
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Maximum Binary Tree
2+
This problem is easy to solve by recursive
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public TreeNode constructMaximumBinaryTree(int[] nums) {
12+
return build(nums, 0, nums.length - 1);
13+
}
14+
15+
public TreeNode build(int[] nums, int beg, int end) {
16+
if (beg > end) return null;
17+
18+
int maxIndex = beg;
19+
for (int i = beg; i <= end; i++) {
20+
if (nums[i] > nums[maxIndex]) maxIndex = i;
21+
}
22+
23+
TreeNode root = new TreeNode(nums[maxIndex]);
24+
root.left = build(nums, beg, maxIndex - 1);
25+
root.right = build(nums, maxIndex + 1, end);
26+
return root;
27+
}
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {number[]} nums
10+
* @return {TreeNode}
11+
*/
12+
var constructMaximumBinaryTree = function(nums) {
13+
return build(nums, 0, nums.length - 1)
14+
};
15+
16+
var build = function(nums, beg, end) {
17+
if (beg > end) return null
18+
19+
let maxIndex = beg
20+
for (let i = beg; i <= end; i++) {
21+
if (nums[maxIndex] < nums[i]) {
22+
maxIndex = i
23+
}
24+
}
25+
26+
let root = new TreeNode(nums[maxIndex])
27+
root.left = build(nums, beg, maxIndex - 1)
28+
root.right = build(nums, maxIndex + 1, end)
29+
return root
30+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def constructMaximumBinaryTree(self, nums):
10+
"""
11+
:type nums: List[int]
12+
:rtype: TreeNode
13+
"""
14+
return self.build(nums, 0, len(nums) - 1)
15+
16+
def build(self, nums, beg, end):
17+
if beg > end:
18+
return None
19+
20+
max_index = beg
21+
for i in xrange(beg, end + 1):
22+
if nums[i] > nums[max_index]:
23+
max_index = i
24+
25+
root = TreeNode(nums[max_index])
26+
root.left = self.build(nums, beg, max_index - 1)
27+
root.right = self.build(nums, max_index + 1, end)
28+
return root

0 commit comments

Comments
 (0)