方法:递归
/**
* 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) {}
* };
*/
class Solution {
private:
TreeNode* solve(vector<int>& nums, int l, int r) {
if (l > r) return NULL;
int mid = l + r >> 1;
TreeNode* nod = new TreeNode(nums[mid]);
nod->left = solve(nums, l, mid-1);
nod->right = solve(nums, mid+1, r);
return nod;
}
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
return solve(nums, 0, nums.size()-1);
}
};
$时间复杂度O(n),空间复杂度O(logn);
方法:迭代
/**
* 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) {}
* };
*/
class Solution {
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
if (!nums.size()) return NULL;
TreeNode* root = new TreeNode(0);
queue<TreeNode*> q;
queue<int> l, r;
l.push(0);
r.push(nums.size()-1);
q.push(root);
while (!q.empty()) {
TreeNode* nod = q.front(); q.pop();
int left = l.front(); l.pop();
int right = r.front(); r.pop();
int mid = left + right >> 1;
nod->val = nums[mid];
if (left <= mid-1) {
nod->left = new TreeNode(0);
q.push(nod->left);
l.push(left);
r.push(mid-1);
}
if (right >= mid + 1) {
nod->right = new TreeNode(0);
q.push(nod->right);
l.push(mid+1);
r.push(right);
}
}
return root;
}
};
$时间复杂度O(n),空间复杂度O(logn);
方法:递归
/**
* 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) {}
* };
*/
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int low, int high) {
if (root == NULL) return NULL;
if (root->val > high) {
TreeNode* left = trimBST(root->left, low, high);
return left;
}
if (root->val < low) {
TreeNode* right = trimBST(root->right, low, high);
return right;
}
root->left = trimBST(root->left, low, high);
root->right = trimBST(root->right, low, high);
return root;
}
};
$时间复杂度O(n),空间复杂度O(n);
方法:迭代
/**
* 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) {}
* };
*/
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int low, int high) {
if (!root) return NULL;
while (root != NULL && (root->val < low || root->val > high)) {
if (root->val > high) root = root->left;
else root = root->right;
}
TreeNode* cur = root;
while (cur != NULL) {
while (cur->left && cur->left->val < low) {
cur->left = cur->left->right;
}
cur = cur->left;
}
cur = root;
while (cur != NULL) {
while (cur->right && cur->right->val > high) {
cur->right = cur->right->left;
}
cur = cur->right;
}
return root;
}
};
$时间复杂度O(n),空间复杂度O(1);
方法:递归
/**
* 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) {}
* };
*/
class Solution {
private:
int pre = 0;
TreeNode* solve(TreeNode* cur) {
if (cur == NULL) return NULL;
solve(cur->right);
cur->val += pre;
pre = cur->val;
solve(cur->left);
return cur;
}
public:
TreeNode* convertBST(TreeNode* root) {
return solve(root);
}
};
$时间复杂度O(n),空间复杂度O(n);
方法:迭代
/**
* 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) {}
* };
*/
class Solution {
public:
TreeNode* convertBST(TreeNode* root) {
stack<TreeNode*> st;
TreeNode* cur = root;
int pre = 0;
while (cur != NULL || !st.empty()) {
if (cur != NULL) {
st.push(cur);
cur = cur->right;
} else {
cur = st.top();
st.pop();
cur->val += pre;
pre = cur->val;
cur = cur->left;
}
}
return root;
}
};
$时间复杂度O(n),空间复杂度O(n);