Code Caprice Algorithm Training Camp 20e jour|654. Arbre binaire maximum|17. Fusionner l'arbre binaire|700. Rechercher dans l'arbre de recherche binaire|98. Vérifier l'arbre de recherche binaire

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    
    
public:
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
    
    
        int maxnum=INT_MIN,index=0;
        for(int i=0;i<nums.size();i++){
    
    
            if(nums[i]>maxnum){
    
    
                maxnum=nums[i];
                index=i;
            }
        }
        TreeNode*root=new TreeNode(maxnum);
        if(nums.size()==0)return root;

        if(index>0){
    
    
                    vector<int>left(nums.begin(),nums.begin()+index);
                    root->left=constructMaximumBinaryTree(left);
        }
        
        if(index<nums.size()-1){
    
    
            vector<int>right(nums.begin()+index+1,nums.end());

            root->right=constructMaximumBinaryTree(right);

        }
        return root;
    }
};
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    
    
public:
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
    
    
        if(root1==nullptr&&root2==nullptr)return NULL;
        TreeNode*root=new TreeNode(0);
        if(root1!=nullptr&&root2!=nullptr){
    
    
            root->val=root1->val+root2->val;
        }else if(root1==nullptr&&root2!=nullptr){
    
    
            return root2;
        }else{
    
    
            return root1;
        }
        root->left=mergeTrees(root1->left,root2->left);
        root->right=mergeTrees(root1->right,root2->right);
        return root;
    }
};
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    
    
public:
    TreeNode* searchBST(TreeNode* root, int val) {
    
    
        if(root==nullptr||val==root->val)return root;
        else if(val>root->val) {
    
    
            return searchBST(root->right,val);
        }
        else if(val<root->val){
    
    
            return searchBST(root->left,val);
        }
        return nullptr;
    }
};
class Solution {
    
    
private:
    vector<int> vec;
    void traversal(TreeNode* root) {
    
    
        if (root == NULL) return;
        traversal(root->left);
        vec.push_back(root->val); // 将二叉搜索树转换为有序数组
        traversal(root->right);
    }
public:
    bool isValidBST(TreeNode* root) {
    
    
        vec.clear(); // 不加这句在leetcode上也可以过,但最好加上
        traversal(root);
        for (int i = 1; i < vec.size(); i++) {
    
    
            // 注意要小于等于,搜索树里不能有相同元素
            if (vec[i] <= vec[i - 1]) return false;
        }
        return true;
    }
};

Supongo que te gusta

Origin blog.csdn.net/weixin_43541510/article/details/132271728
Recomendado
Clasificación