LeetCodeDay17


LeetCode654.最大二叉树

class Solution {
public:
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        if(nums.empty())return nullptr;
        int maxIndex;
        int maxNum = INT_MIN;
        for(int i = 0; i < nums.size(); i++){
            if(nums[i] > maxNum){
                maxIndex = i;
                maxNum = nums[i];
            }
        }
        TreeNode* root = new TreeNode();
        root->val = maxNum;
        vector<int> leftnums(nums.begin(), nums.begin() + maxIndex);
        vector<int> rightnums(nums.begin() + maxIndex + 1, nums.end());
        root->left = constructMaximumBinaryTree(leftnums);
        root->right = constructMaximumBinaryTree(rightnums);
        return root;
    }
};

LeetCode617.合并二叉树

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

LeetCode700.二叉搜索树中的搜索

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

LeetCode98.验证二叉树

class Solution {
public:
    bool isValidBST(TreeNode* root) {
        return isValidBST_(root, INT64_MAX, INT64_MIN);//这里防止数据溢出,有一些用例会这样
    }
    bool isValidBST_(TreeNode* root,long long int upper,long long int lower){
        if(!root)return true;
        else{
            if(!root->left && !root->right){
                return true;
            }
            else if(root->left && !root->right){
                if(root->val > root->left->val && root->left->val > lower && root->val < upper){
                    return isValidBST_(root->left, root->val, lower);
                }
            }else if(root->right && !root->left){
                if(root->val < root->right->val && root->right->val < upper && root->val > lower){
                    return isValidBST_(root->right, upper, root->val);
                }
            }else if(root->left && root->right){
                if(root->val < root->right->val && root->val > root->left->val && root->left->val >lower && root->right->val<upper){
                    return isValidBST_(root->right, upper, root->val) && isValidBST_(root->left, root->val, lower);
                }
            }
            return false;
        }
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值