-
Notifications
You must be signed in to change notification settings - Fork 43
/
maximum-binary-tree.py
100 lines (82 loc) · 3 KB
/
maximum-binary-tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
654. Maximum Binary Tree
Medium
You are given an integer array nums with no duplicates. A maximum binary tree can be built recursively from nums using the following algorithm:
Create a root node whose value is the maximum value in nums.
Recursively build the left subtree on the subarray prefix to the left of the maximum value.
Recursively build the right subtree on the subarray suffix to the right of the maximum value.
Return the maximum binary tree built from nums.
Example 1:
Input: nums = [3,2,1,6,0,5]
Output: [6,3,5,null,2,0,null,null,1]
Explanation: The recursive calls are as follow:
- The largest value in [3,2,1,6,0,5] is 6. Left prefix is [3,2,1] and right suffix is [0,5].
- The largest value in [3,2,1] is 3. Left prefix is [] and right suffix is [2,1].
- Empty array, so no child.
- The largest value in [2,1] is 2. Left prefix is [] and right suffix is [1].
- Empty array, so no child.
- Only one element, so child is a node with value 1.
- The largest value in [0,5] is 5. Left prefix is [0] and right suffix is [].
- Only one element, so child is a node with value 0.
- Empty array, so no child.
Example 2:
Input: nums = [3,2,1]
Output: [3,null,2,null,1]
Constraints:
1 <= nums.length <= 1000
0 <= nums[i] <= 1000
All integers in nums are unique.
"""
# V0
# V1
# http://bookshadow.com/weblog/2017/08/06/leetcode-maximum-binary-tree/
# IDEA :
# -> STEP 1) FIND THE INDEX OF MAX ELEMENT IN THE ARRAY
# -> STEP 2) SET THE MAX ELEMENT AS ROOT
# -> STEP 3) SPLIT THE ARRAY INTO LEFT AND RIGHT ON THE INDEX (OF MAX ELEMENT )
# -> STEP 4) SET MAX ELEMENT IN LEFT ARRAY AS LEFT SUB TEREE
# -> STEP 5) SET MAX ELEMENT IN RIGHT ARRAY AS RIGHT SUB TEREE
# -> REPEAT STEP 3) - 5)
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def constructMaximumBinaryTree(self, nums):
"""
:type nums: List[int]
:rtype: TreeNode
"""
if not nums: return None
maxn = max(nums)
idx = nums.index(maxn)
node = TreeNode(maxn)
node.left = self.constructMaximumBinaryTree(nums[:idx])
node.right = self.constructMaximumBinaryTree(nums[idx+1:])
return node
# V2
# Time: O(n)
# Space: O(n)
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def constructMaximumBinaryTree(self, nums):
"""
:type nums: List[int]
:rtype: TreeNode
"""
# https://github.com/kamyu104/LintCode/blob/master/C++/max-tree.cpp
nodeStack = []
for num in nums:
node = TreeNode(num)
while nodeStack and num > nodeStack[-1].val:
node.left = nodeStack.pop()
if nodeStack:
nodeStack[-1].right = node
nodeStack.append(node)
return nodeStack[0]