第19天打卡:我做的越来越麻烦...

1.Maximum Binary Tree

这题和昨天通过中序和前序构造二叉树的题差不多啊。只是把规则抽象了出来。

class Solution {
public:
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        //0. check if it's leaf node
        //1. find max
        //2. assign left and right leaf respectively.

        int size = nums.size();

        if(size == 0) return nullptr;
        if(size == 1){
            return new TreeNode(nums[0]);
        }

        int idx = 0;
        for(int i = 0; i < size; ++i){
            if(nums[i] > nums[idx]) idx = i;
        }

        TreeNode* root = new TreeNode(nums[idx]);

        vector<int> vleft(nums.begin(), nums.begin()+idx);
        vector<int> vright(nums.begin()+idx+1, nums.end());

        root->left = constructMaximumBinaryTree(vleft);
        root->right = constructMaximumBinaryTree(vright);

        return root;
    }
};

2.Merge Two Binary Trees

这题花了我挺长时间的,很烦。但居然是个easy…心累。

class Solution {
private:
    TreeNode* count(TreeNode* root){
        if(!root) return nullptr;
        TreeNode* tmp = new TreeNode(root->val);
        tmp->left = count(root->left);
        tmp->right = count(root->right);
        return tmp;
    }

    TreeNode* merge(TreeNode* root1, TreeNode* root2){
        //condition checking
        if(!root1 && !root2) return nullptr;
        if(!root1 && root2){
            return count(root2);
        }

        if(!root2){
            return count(root1);
        }

        TreeNode* root = new TreeNode(root1->val + root2->val);
        root->left = merge(root1->left, root2->left);
        root->right = merge(root1->right, root2->right);
        return root;
    }
public:
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
        if(!root1 && !root2) return nullptr;
        if(!root1) return root2;
        if(!root2) return root1;
        return merge(root1, root2);
    }
};

原来可以in-place这样改…没想到啊。最后return root1/root2 相当于直接接过那个还没走完的树了。

class Solution {
public:
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
        if(!root1) return root2;
        if(!root2) return root1;

        root1->val += root2->val;
        root1->left = mergeTrees(root1->left, root2->left);
        root1->right = mergeTrees(root1->right, root2->right);
        return root1;
    }
};

3.Search in a Binary Search Tree
递归自然是很简单:

class Solution {
public:
    TreeNode* searchBST(TreeNode* root, int val) {
        if(!root) return nullptr;
        if(root->val == val) return root;
        return root->val > val? searchBST(root->left, val) : searchBST(root->right, val);
    }
};

iteration:

class Solution {
public:
    TreeNode* searchBST(TreeNode* root, int val) {
        while(root){
            if(root->val == val) return root;
            if(root->val > val){
                root = root->left;
            }else{
                root = root->right;
            }
        }
        return nullptr;
    }
};

两个版本都很简单。

4.Validate Binary Search Tree

记得一刷这道题的时候,还很笨,想不出来“看中序遍历数组是否为升序”这样的办法。现在进步了!

class Solution {
private:
    vector<int> res;
    void inorder(TreeNode* root){
        if(!root) return;
        inorder(root->left);
        res.push_back(root->val);
        inorder(root->right);
    }
public:
    bool isValidBST(TreeNode* root) {
        inorder(root);

        int t = res[0];
        for(int i = 1; i < res.size(); ++i){
            if(t >= res[i]) return false;
            t = res[i];
        }
        return true;
    }   
};

与此同时,应该还有不同的解法… 但这个应该最直观!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值