654. 最大二叉树
题目链接:654. 最大二叉树 - 力扣(LeetCode)
视频讲解:又是构造二叉树,又有很多坑!| LeetCode:654.最大二叉树_哔哩哔哩_bilibili
文章讲解:代码随想录 (programmercarl.com)
看视频前自己搞定啦
/**
* 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) {
if(nums.size()==0) return NULL;
int rootValue = nums[0];
int index = 0;
for(int i=0; i<nums.size(); i++){
if(nums[i]>rootValue){
index = i;
rootValue = nums[i];
}
}
TreeNode* root = new TreeNode(rootValue);
if(nums.size()==1) return root;
// 构建左子树数组
vector<int> leftNums(nums.begin(), nums.begin()+index);
// 构建右子树数组
vector<int> rightNums(nums.begin()+index+1, nums.end());
//递归
root->left = constructMaximumBinaryTree(leftNums);
root->right = constructMaximumBinaryTree(rightNums);
return root;
}
};
617. 合并二叉树
题目链接:617. 合并二叉树 - 力扣(LeetCode)
视频讲解:一起操作两个二叉树?有点懵!| LeetCode:617.合并二叉树_哔哩哔哩_bilibili
文章讲解:代码随想录 (programmercarl.com)
/**
* 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==NULL) return root2;
if(root2==NULL) return root1;
// 单层递归逻辑
// 在tree1的基础上改结构
root1->val += root2->val;
root1->left = mergeTrees(root1->left, root2->left);
root1->right = mergeTrees(root1->right, root2->right);
return root1;
}
};
700. 二叉搜索树中的搜索
题目链接:700. 二叉搜索树中的搜索 - 力扣(LeetCode)
视频讲解:不愧是搜索树,这次搜索有方向了!| LeetCode:700.二叉搜索树中的搜索_哔哩哔哩_bilibili
文章讲解:代码随想录 (programmercarl.com)
二叉搜索树是一个有序树:
- 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
- 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
- 它的左、右子树也分别为二叉搜索树
这就决定了,二叉搜索树,递归遍历和迭代遍历和普通二叉树都不一样。
本题,其实就是在二叉搜索树中搜索一个节点。那么我们来看看应该如何遍历。
递归法
/**
* 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) {
if(root==NULL || root->val==val) return root;
TreeNode *result;
if(val < root->val) result = searchBST(root->left, val);
if(val > root->val) result = searchBST(root->right, val);
return result;
}
};
迭代法
/**
* 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!=NULL){
if(val < root->val) root = root->left;
else if(val > root->val) root = root->right;
else return root;
}
return NULL;
}
};
98. 验证二叉搜索树
题目链接:98. 验证二叉搜索树 - 力扣(LeetCode)
视频讲解:你对二叉搜索树了解的还不够! | LeetCode:98.验证二叉搜索树_哔哩哔哩_bilibili
文章讲解:代码随想录 (programmercarl.com)
对于二叉搜索树,采用中序遍历时,元素是单调递增的
/**
* 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:
long long maxVal = LONG_MIN; // 因为后台测试数据中有int最小值
bool isValidBST(TreeNode* root) {
if(root==NULL) return true;
// 开始中序遍历
bool left = isValidBST(root->left);
if(root->val > maxVal){
maxVal = root->val;
}
else return false;
bool right = isValidBST(root->right);
return left && right;
}
};
双指针优化
/**
* 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 *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;
}
};