1.最大二叉树
https://leetcode.cn/problems/maximum-binary-tree/submissions/
public:
//以最大值为分割点,划分左右子树的元素区间
// TreeNode* maxTree(vector<int>& nums){
// //1.终止条件 如果区间为空
// if(nums.size() == 0) return nullptr;
// //1.1遍历数组找到最大值及其下标 生成根节点
// int max =0;
// int index = 0;
// for(int i = 0;i<nums.size();i++){
// if(nums[i]>max){
// max = nums[i];
// index = i;
// }
// }
// TreeNode* root = new TreeNode(max);
// //3.确定区间
// //左区间 左闭右开 此处可以优化,传递索引,减少创建数组的消耗
// vector<int> numsleft(nums.begin(),nums.begin()+index);
// //右区间 左闭右开
// vector<int> numsright(nums.begin()+index+1,nums.end());
// //4.递归逻辑
// //左
// root->left = maxTree(numsleft);
// root->right = maxTree(numsright);
// return root;
// }
TreeNode* maxTree(vector<int>& nums,int left,int right){//用索引来分割nums数组
//1.终止条件 因为right这个索引是取不到的(左闭又开),所以当left>=right的时候要返回
if(left>=right) return nullptr;
//1.1遍历数组找到最大值及其下标 生成根节点
//left right 分别代表传入的数组的左右索引
int maxvalue = nums[left];
int maxindex = left;
for(int i = left+1;i<right;i++){//注意此判定条件 乃是right
if(nums[i] > maxvalue){
maxvalue = nums[i];
maxindex = i;
}
}
TreeNode* root = new TreeNode(maxvalue);
//3.确定区间 左闭右开
//4.递归逻辑
root->left = maxTree(nums,left,maxindex);
root->right = maxTree(nums,maxindex+1,right);
return root;
}
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
// return maxTree(nums);
return maxTree(nums,0,nums.size());
}
};
2.合并二叉树
https://leetcode.cn/problems/merge-two-binary-trees/submissions/
class Solution {
public:
//1.两棵树对应的节点都存在,数值相加
//2.只有一颗树的界定啊存在,直接作为新节点
//3.都不存在,那就不存在
//前序遍历
//
//
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
//确定终止条件 右空树就返回另一棵树
if(root1 == nullptr) return root2;
if(root2 == nullptr) return root1;
//单层逻辑
root1->val = root1->val + root2->val;
//同时向两个树的同一方向遍历 并将其赋给其中一棵树
root1->left = mergeTrees(root1->left,root2->left);
root1->right = mergeTrees(root1->right,root2->right);
return root1;
}
};
3.二叉搜索树中的搜索
https://leetcode.cn/problems/search-in-a-binary-search-tree/submissions/
class Solution {
public:
TreeNode* cur;//
//采用中序遍历
TreeNode* searchBST(TreeNode* root, int val) {
//1.递归法
//终止条件:遍历到空节点时返回空
// if(root == nullptr) return nullptr;
// //中 判断当前节点是否为搜索的节点
// if(root->val == val) return root;
// //二叉搜索树为有序树 注意这个的返回方式
// if(root->val > val) return searchBST(root->left,val);
// else return searchBST(root->right,val) ;
// return nullptr;
//2.迭代法
//循环元素为root,每次遍历后将root根据大小变为其左或右子树,直到为空或满足条件
while (root != NULL) {
if (root->val > val) root = root->left;
else if (root->val < val) root = root->right;
else return root;
}
return NULL;
}
};
4.验证二叉搜索树
https://leetcode.cn/problems/validate-binary-search-tree/submissions/
class Solution {
public:
//当前节点为空返回true
//否则,比较其左右子树 都满足条件后,返回true
//因为数值大小是按照左中右的顺序,所以采用中序遍历
// long long maxVal = LONG_MIN;// 因为后台测试数据中有int最小值
// bool isValidBST(TreeNode* root) {
// //终止条件:当前节点为空返回true
// if(!root) return true;
// //左遍历
// bool leftbool = isValidBST(root->left);
// //中
// if(root->val > maxVal) maxVal = root->val;
// else return false;
// bool rightbool = isValidBST(root->right);
// // return leftbool && rightbool ;
// }
TreeNode* pre = NULL; // 用来记录前一个节点
bool isValidBST(TreeNode* root) {
if (root == NULL) return true;
bool left = isValidBST(root->left);
if (pre != NULL && pre->val >= root->val) return false;
pre = root; // 记录前一个节点
bool right = isValidBST(root->right);
return left && right;
}
};