654 最大二叉树
题目链接:https://leetcode.cn/problems/maximum-binary-tree/description/
/**
* 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* constructMaximumBinaryTree(vector<int>& nums) {
TreeNode* node = new TreeNode(0); //定义一个新节点,并将这个数组的数值赋给新的节点,然后返回这个节点。
if (nums.size() == 1){
node->val = nums[0];
return node;
}
//找到数组中最大的值和对应下标
int maxValue = 0;
int maxValueIndex = 0;
for (int i = 0; i<nums.size(); i++){
if (nums[i] >maxValue){
maxValue = nums[i];
maxValueIndex = i;
}
}
node->val =maxValue;
// 最大值所在的下标左区间 构造左子树
if(maxValueIndex>0){
//重新定义数组
vector<int> newVec(nums.begin(), nums.begin() + maxValueIndex);
node->left = constructMaximumBinaryTree(newVec);
}
//最大值所在的下标右区间 构造右子树
if(maxValueIndex<(nums.size() - 1)){
vector<int> newVec(nums.begin() + maxValueIndex + 1, nums.end());
node->right = constructMaximumBinaryTree(newVec);
}
return node;
}
};
617 合并二叉树
题目链接:https://leetcode.cn/problems/merge-two-binary-trees/
/**
* 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* mergeTrees(TreeNode* root1, TreeNode* root2) {
if(root1 == nullptr) return root2;
if(root2 == nullptr) return root1;
TreeNode* rt = new TreeNode(0);
//root1和root2都不为空 修改root1的数值与结构
rt->val = root1->val + root2->val;//递归 中左右
rt->left = mergeTrees(root1->left, root2->left);
rt->right = mergeTrees(root1->right, root2->right);
return rt;
}
};
700 二叉搜索树中的搜索
https://leetcode.cn/problems/search-in-a-binary-search-tree/description/
/**
* 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* searchBST(TreeNode* root, int val) {
while (root != nullptr){
if(root->val > val) root = root->left;
else if (root->val < val) root = root->right;
else return root;
}
return nullptr;
}
};
98 验证二叉搜索树
https://leetcode.cn/problems/validate-binary-search-tree/
/**
* 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:
vector<int> vec;
void traversal(TreeNode* root){
if(root == nullptr) return; //中序遍历
traversal(root->left);
vec.push_back(root->val);
traversal(root->right);
}
public:
bool isValidBST(TreeNode* root) {
vec.clear();
traversal(root);
for(int i = 1; i<vec.size(); i++){
//小于等于保证不重复
if(vec[i]<=vec[i-1])
return false;
}
return true;
}
};