-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path108.convert-sorted-array-to-binary-search-tree.cpp
More file actions
47 lines (45 loc) · 1.44 KB
/
Copy path108.convert-sorted-array-to-binary-search-tree.cpp
File metadata and controls
47 lines (45 loc) · 1.44 KB
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
/*
* @lc app=leetcode id=108 lang=cpp
*
* [108] Convert Sorted Array to Binary Search Tree
*/
// @lc code=start
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
#include "bits/stdc++.h"
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
// edge case for DFS
if (nums.size() == 0) return nullptr;
if (nums.size() == 1) return new TreeNode(nums[0]);
int half = nums.size() / 2;
TreeNode *root = new TreeNode(nums[half]);
vector<int> left(half, 0);
std::copy(nums.begin(), nums.begin() + half, left.begin());
vector<int> right(nums.size() - half - 1, 0);
std::copy(nums.begin() + half + 1, nums.end(), right.begin());
root->left = sortedArrayToBST(left);
root->right = sortedArrayToBST(right);
return root;
}
};
// @lc code=end