代码随想录算法训练营DAY17第六章 二叉树 part05

目录

[654. 最大二叉树](#654. 最大二叉树)

[617. 合并二叉树](#617. 合并二叉树)

[700. 二叉搜索树中的搜索](#700. 二叉搜索树中的搜索)

[98. 验证二叉搜索树](#98. 验证二叉搜索树)


654. 最大二叉树

cpp 复制代码
/**
 * 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 {
    TreeNode* construct(vector<int>&nums,int left,int right){
        if(left>right)return nullptr;
        int best=left;
        for(int i=left+1;i<=right;i++){
            if(nums[i]>nums[best])best=i;
        }
        TreeNode* node=new TreeNode(nums[best]);
        node->left=construct(nums,left,best-1);
        node->right=construct(nums,best+1,right);
        return node;
    }
public:
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        return construct(nums,0,nums.size()-1);
    }
};

617. 合并二叉树

cpp 复制代码
/**
 * 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)return root2;
        if(!root2)return root1;
        auto merged=new TreeNode(root1->val+root2->val);
        merged->left=mergeTrees(root1->left,root2->left);
        merged->right=mergeTrees(root1->right,root2->right);
        return merged;
    }
};

700. 二叉搜索树中的搜索

cpp 复制代码
/**
 * 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)return nullptr;
        if(val==root->val)return root;
        if(val<root->val)return searchBST(root->left,val);
        else return searchBST(root->right,val);
    }
};

98. 验证二叉搜索树

cpp 复制代码
/**
 * 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 {
    long long pre=LLONG_MIN;
public:
    bool isValidBST(TreeNode* root) {
        if(!root)return true;
        if(!isValidBST(root->left))return false;
        if(root->val<=pre)return false;
        pre=root->val;
        return isValidBST(root->right);
    }
};
相关推荐
m0_672703311 小时前
上机练习第51天
数据结构·c++·算法
仰泳的熊猫2 小时前
题目2577:蓝桥杯2020年第十一届省赛真题-走方格
数据结构·c++·算法·蓝桥杯
灰色小旋风2 小时前
力扣13 罗马数字转整数
数据结构·c++·算法·leetcode
ccLianLian4 小时前
数论·欧拉函数
数据结构·算法
会编程的土豆4 小时前
C++中的 lower_bound 和 upper_bound:一篇讲清楚
java·数据结构·算法
HUTAC5 小时前
关于进制转换及其应用的算法题总结
数据结构·c++·算法
XW01059995 小时前
6-函数-1 使用函数求特殊a串数列和
数据结构·python·算法
沉鱼.445 小时前
枚举问题集
java·数据结构·算法
罗超驿5 小时前
Java数据结构_栈_算法题
java·数据结构·
️是786 小时前
信息奥赛一本通—编程启蒙(3346:【例60.3】 找素数)
数据结构·c++·算法