-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmaximum-binary-tree.cpp
More file actions
32 lines (27 loc) · 878 Bytes
/
Copy pathmaximum-binary-tree.cpp
File metadata and controls
32 lines (27 loc) · 878 Bytes
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
/**
* 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 helper(nums, 0, nums.size() - 1);
}
//max_index denotes the index of the maximum number in range [left, right]
TreeNode* helper(vector<int>& nums, int left, int right){
if(left>right)return NULL;
int max_index = left;
for(int i = left; i<=right; i++){
if(nums[i] > nums[max_index])max_index = i;
}
TreeNode* root = new TreeNode(nums[max_index]);
root->left = helper(nums, left, max_index - 1);
root->right = helper(nums, max_index + 1, right);
return root;
}
};