700.
- 反了个方向,注意!!!
//这个就是对的
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if(root == nullptr || root->val == val) return root;
if(root->val > val) return searchBST(root->left, val);
if(root->val < val) return searchBST(root->right, val);
return nullptr;
}
};
//这个错了??
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if(root==nullptr) return nullptr;
if(root->val == val){
return root;
} else if(root->val < val){
return searchBST(root->left, val);
}else{
return searchBST(root->right, val);
}
}
};
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if(root==nullptr) return nullptr;
if(root->val == val){
return root;
} else if(root->val < val){
return searchBST(root->right, val);
}else{
return searchBST(root->left, val);
}
}
};
701.
思想未get到??
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
if(root == nullptr) return new TreeNode(val);
if(root->val > val){
root->left = insertIntoBST(root->left, val);
}else{
root->right = insertIntoBST(root->right, val);
}
return root;
}
};
654
class Solution {
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
auto maxPosition = max_element(nums.begin(), nums.end());
int mark = maxPosition-nums.begin();
TreeNode* root = new TreeNode(*maxPosition);
vector<int> l, r;
copy(nums.begin(), maxPosition-1, l.begin());
copy(maxPosition+1, nums.end(), r.begin());
root->left = constructMaximumBinaryTree(l);
root->right = constructMaximumBinaryTree(r);
return root;
}
};
//以上错的