-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy path654. Maximum Binary Tree.cpp
40 lines (38 loc) · 1.09 KB
/
654. Maximum Binary Tree.cpp
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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
return DFS(nums, 0, nums.size());
}
TreeNode* DFS(vector<int>& nums, int l, int r){
if(l == r) return NULL;
int maxPos = l;
for(int i = l; i < r; i++) if(nums[i] > nums[maxPos]) maxPos = i;
TreeNode* root = new TreeNode(nums[maxPos]);
root->left = DFS(nums, l, maxPos);
root->right = DFS(nums, maxPos + 1, r);
return root;
}
};
// O(n)
class Solution {
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
deque<TreeNode*>q;
for(auto x: nums){
TreeNode* p = new TreeNode(x);
while(!q.empty() && q.back()->val < x) p->left = q.back(), q.pop_back();
if(!q.empty() && q.back()->val > x) q.back()->right = p;
q.push_back(p);
}
return q.front();
}
};