代码随想录二刷Day20
今日任务
654.最大二叉树
617.合并二叉树
700.二叉搜索树中的搜索
98.验证二叉搜索树
语言:C++
654. 最大二叉树
链接:https://leetcode.cn/problems/maximum-binary-tree/
前序遍历
class Solution {
public:
//[left, right]
TreeNode* construct(vector<int>& nums, int left, int right){
if(right - left == 0){
TreeNode* root = new TreeNode(nums[left]);
return root;
}
int max = left;
for(int i = left + 1; i <= right; i++){
max = nums[max] > nums[i] ? max : i;
}
TreeNode* root = new TreeNode(nums[max]);
if(left <= max - 1) root->left = construct(nums, left, max - 1);
if(max + 1 <= right) root->right = construct(nums, max + 1, right);
return root;
}
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
return construct(nums, 0, nums.size() - 1);
}
};
617. 合并二叉树
链接:https://leetcode.cn/problems/merge-two-binary-trees/
递归
class Solution {
public:
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
if(root1 == NULL && root2 == NULL) return NULL;
if(root1 == NULL && root2 != NULL) return root2;
if(root1 != NULL && root2 == NULL) return root1;
root1->val = root1->val + root2->val;
root1->left = mergeTrees(root1->left, root2->left);
root1->right = mergeTrees(root1->right, root2->right);
return root1;
}
};
迭代
class Solution {
public:
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
if(root1 == NULL && root2 == NULL) return NULL;
if(root1 == NULL && root2 != NULL) return root2;
if(root1 != NULL && root2 == NULL) return root1;
queue<TreeNode*> que;
que.push(root1);
que.push(root2);
while(!que.empty()){
TreeNode* node1 = que.front();
que.pop();
TreeNode* node2 = que.front();
que.pop();
node1->val += node2->val;
if(node1->left != NULL && node2->left != NULL){
que.push(node1->left);
que.push(node2->left);
}
if(node1->right != NULL && node2->right != NULL){
que.push(node1->right);
que.push(node2->right);
}
if(node1->left == NULL && node2->left != NULL){
node1->left = node2->left;
}
if(node1->right == NULL && node2->right != NULL){
node1->right = node2->right;
}
//下面两种情况不需要考虑,因为相当于node1没有发生变化
//node1->left != NULL && node2->left == NULL
//node1->right != NULL && node2->right == NULL
}
return root1;
}
};
700. 二叉搜索树中的搜索
链接:https://leetcode.cn/problems/search-in-a-binary-search-tree/
递归
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if(root == NULL) return NULL;
if(root->val == val) return root;
if(val < root->val) return searchBST(root->left, val);
if(val > root->val) return searchBST(root->right, val);
return NULL;
}
};
迭代
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if(root->val == val) return root;
TreeNode* cur = root;
while(cur){
if(cur == NULL) break;
else if(val < cur->val) cur = cur->left;
else if(val > cur->val) cur = cur->right;
else if(val == cur->val) break;
}
return cur;
}
};
98. 验证二叉搜索树
链接:https://leetcode.cn/problems/validate-binary-search-tree/
陷阱:不可以只验证当前子树的父子节点间是否满足二叉搜索树特性,有可能隔着几个节点发现条件不满足了
二叉搜索树按照中序遍历会得到单调递增的数组
递归
class Solution {
public:
vector<int> res;
void traversal(TreeNode* root){
if(root == NULL) return;
traversal(root->left);
res.push_back(root->val);
traversal(root->right);
}
bool isValidBST(TreeNode* root) {
traversal(root);
for(int i = 1; i < res.size(); i++){
if(res[i] <= res[i - 1]) return false;
}
return true;
}
};
迭代
class Solution {
public:
vector<int> res;
bool isValidBST(TreeNode* root) {
stack<TreeNode*> st;
st.push(root);
while(!st.empty()){
TreeNode* cur = st.top();
if(cur != NULL){
st.pop();
if(cur->right) st.push(cur->right);
st.push(cur);
st.push(NULL);
if(cur->left) st.push(cur->left);
}
else{
st.pop();
cur = st.top();
st.pop();
res.push_back(cur->val);
}
}
for(int i = 1; i < res.size(); i++){
if(res[i] <= res[i - 1]) return false;
}
return true;
}
};