day20打卡
时间复杂度:O(N^2),空间复杂度:O(N)
class Solution {
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
//[left, right]闭区间
return construct(nums, 0, nums.size()-1);
}
TreeNode* construct(vector<int>& nums, int left, int right)
{
//递归出口
if(left > right) return nullptr;
//设置最大值的下标
int maxValIndex = left;
//更新最大值的下标
for(int i = left+1; i <= right; i++)
{
if(nums[i] > nums[maxValIndex]) maxValIndex = i;
}
TreeNode* newNode = new TreeNode(nums[maxValIndex]);
//递归创建左子树
newNode->left = construct(nums, left, maxValIndex-1);
//递归创建右子树
newNode->right = construct(nums, maxValIndex+1, right);
return newNode;
}
};
-
递归法
-
时间复杂度:O(N),空间复杂度:O(N)
class Solution {
public:
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
//递归出口
//t1为空,节点就是t2,反之同理
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;
}
};
- 迭代法
- 时间复杂度:O(N),空间复杂度:O(N)
class Solution {
public:
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
if(root1 == nullptr) return root2;
if(root2 == nullptr) return root1;
queue<TreeNode*> q;
q.push(root1);
q.push(root2);
while(!q.empty())
{
TreeNode* left = q.front();
q.pop();
TreeNode* right = q.front();
q.pop();
left->val += right->val;
if(left->left != nullptr && right->left != nullptr)
{
q.push(left->left);
q.push(right->left);
}
if(left->right != nullptr && right->right != nullptr)
{
q.push(left->right);
q.push(right->right);
}
if(left->left == nullptr && right->left != nullptr)
{
left->left = right->left;
}
if(left->right == nullptr && right->right != nullptr)
{
left->right = right->right;
}
}
return root1;
}
};
二叉树搜索树的性质:左子树所有节点的元素值均小于根的元素值; 右子树所有节点的元素值均大于根的元素值。
- 递归
- 时间复杂度:O(N),空间复杂度:O(N)
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
//递归出口
if(root == nullptr || root->val == val) return root;
//子问题
if(root->val > val) return searchBST(root->left, val);
if(root->val < val) return searchBST(root->right, val);
return nullptr;
}
};
- 迭代
- 时间复杂度:O(N),空间复杂度:O(1)
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;
}
};
- 递归法
- 时间复杂度:O(N),空间复杂度:O(N)
class Solution {
public:
long preNum = LONG_MIN;
bool isValidBST(TreeNode* root) {
return dfs(root);
}
bool dfs(TreeNode* root)
{
//出口
//空树叶符合搜索二叉树
if(root == nullptr) return true;
bool left = dfs(root->left);
//剪枝
//左树必须小于当前节点的val
if(left == false || preNum >= root->val) return false;
//记录当前节点val
preNum = root->val;
bool right = dfs(root->right);
return left && right;
}
};
- 迭代法
- 时间复杂度:O(N),空间复杂度:O(N)
class Solution {
public:
bool isValidBST(TreeNode* root) {
stack<TreeNode*> st;
TreeNode* cur = root;
TreeNode* pre = nullptr;
while(!st.empty() || cur != nullptr)
{
if(cur != nullptr)
{
st.push(cur);
cur = cur->left;
}
else
{
cur = st.top();
st.pop();
if(pre != nullptr && cur->val <= pre->val) return false;
pre = cur;
cur = cur->right;
}
}
return true;
}
};