代码随想录刷题第19天
654. 最大二叉树
/**
* 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 * transation(vector<int>& nums){
int tmp = nums[0];
int index = 0;
for(int i = 1; i < nums.size(); i++){
if(nums[i] > tmp){
tmp = nums[i];
index = i;
}
}
TreeNode * root = new TreeNode(nums[index]);
vector<int> leftboard(nums.begin(),nums.begin()+index);//左闭右开的初始化区间
vector<int> rightboard(nums.begin()+index+1,nums.end());
if(leftboard.size()!=0){
root->left =transation(leftboard);
}
if(rightboard.size()!=0){
root->right = transation(rightboard);
}
return root;
}
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
TreeNode *a = transation(nums);
return a;
}
};
617. 合并二叉树
/**
* 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* traversal(TreeNode* root1, TreeNode* root2){
if(root1 && !root2) return root1;
else if(!root1 && root2) return root2;
else if(!root2 && !root1) return nullptr;
int value = root2->val + root1->val;
TreeNode *node = new TreeNode(value);
node->left= traversal(root1->left, root2->left);
node->right= traversal(root1->right, root2->right);
return node;
}
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
TreeNode *result = traversal(root1,root2);
return result;
}
};
700. 二叉搜索树中的搜索
/**
* 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 *result = nullptr;
void traversal(TreeNode* root, int val){
if(root == nullptr) return ;
if(root->val == val) {
result = root;
}
traversal(root->left, val);
traversal(root->right, val);
}
TreeNode* searchBST(TreeNode* root, int val) {
traversal(root, val);
return result;
}
};
98. 验证二叉搜索树
/**
* 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:
vector<int> nums;
void traversal(TreeNode *root){
if(root == nullptr) return ;
traversal(root->left);
nums.push_back(root->val);
traversal(root->right);
}
bool isValidBST(TreeNode* root) {
bool result = true;
traversal(root);
for(int i = 1; i < nums.size(); i++){
if(nums[i] <= nums[i-1]){
result = false;
}
}
return result;
}
};