day20打卡

day20打卡

654. 最大二叉树

时间复杂度: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;
    }
};

617. 合并二叉树

  • 递归法

  • 时间复杂度: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;
    }
};

700. 二叉搜索树中的搜索

二叉树搜索树的性质:左子树所有节点的元素值均小于根的元素值; 右子树所有节点的元素值均大于根的元素值。

  • 递归
  • 时间复杂度: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;
    }
};

98. 验证二叉搜索树

  • 递归法
  • 时间复杂度: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;
    }
};
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值