代码随想录第17天, 654.最大二叉树、 617.合并二叉树、700.二叉搜索树中的搜索、98.验证二叉搜索树

654.最大二叉树

// 最大二叉树

#include <vector>
#include <iostream>

using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

TreeNode *coustructMaxTree(vector<int> nums)
{
    TreeNode *node = new TreeNode(0);
    if (nums.size() == 1)
    {
        node->val = nums[0];
        return node;
    }

    int maxValue = 0;
    int maxIndex = 0;
    for (int i = 0; i < nums.size(); i++)
    {
        if (nums[i] > maxValue)
        {
            maxValue = nums[i];
            maxIndex = i;
        }
    }
    node->val = maxValue;
    if (maxIndex > 0)
    {
        vector<int> newvec(nums.begin(), nums.begin() + maxIndex);
        node->left = coustructMaxTree(newvec);
    }
    if (maxIndex < (nums.size() - 1))
    {
        vector<int> newvec(nums.begin() + maxIndex + 1, nums.end());
        node->right = coustructMaxTree(newvec);
    }

    return node;
}

void printPreorder(TreeNode *node)
{
    if (node == nullptr)
    {
        return; // 如果节点为空,则返回
    }

    std::cout << node->val << " "; // 首先打印根节点的值
    printPreorder(node->left);     // 递归打印左子树
    printPreorder(node->right);    // 递归打印右子树
}

int main()
{
    std::vector<int> nums = {3, 2, 1, 6, 0, 5};
    TreeNode *root = coustructMaxTree(nums);
    printPreorder(root);
    std::cout << std::endl;
    return 0;
}

617.合并二叉树

// 合并二叉树

#include <iostream>

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

TreeNode *mergeTrees(TreeNode *t1, TreeNode *t2)
{
    if (t1 == NULL)
        return t2;
    if (t2 == NULL)
        return t1;

    TreeNode *root = new TreeNode(0);
    root->val = t1->val + t2->val;

    root->left = mergeTrees(t1->left, t2->left);
    root->right = mergeTrees(t1->right, t2->right);

    return root;
}

void printpreorder(TreeNode *root)
{
    if (root == NULL)
        return;
    std::cout << root->val << ' ';
    printpreorder(root->left);
    printpreorder(root->right);
}

int main()
{
    TreeNode *t1 = new TreeNode(1);
    t1->left = new TreeNode(3);
    t1->left->left = new TreeNode(5);
    t1->right = new TreeNode(2);

    TreeNode *t2 = new TreeNode(2);
    t2->left = new TreeNode(1);
    t2->left->right = new TreeNode(4);
    t2->right = new TreeNode(3);
    t2->right->right = new TreeNode(7);

    TreeNode *root = mergeTrees(t1, t2);
    printpreorder(root);
    return 0;
}

 700.二叉搜索树中的搜索

// 二叉搜索树的搜索

#include <iostream>

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

TreeNode *searchBST(TreeNode *root, int val)
{
    if (root == NULL || root->val == val)
        return root;
    TreeNode *res = NULL;
    if (root->val > val)
        res = searchBST(root->left, val);
    if (root->val < val)
        res = searchBST(root->right, val);
    return res;
}

void printpreorder(TreeNode *root)
{
    if (root == NULL)
        return;
    std::cout << root->val << ' ';
    printpreorder(root->left);
    printpreorder(root->right);
}

int main()
{
    TreeNode *t2 = new TreeNode(4);
    t2->left = new TreeNode(2);
    t2->right = new TreeNode(7);
    t2->left->left = new TreeNode(1);
    t2->left->right = new TreeNode(3);

    TreeNode *root = searchBST(t2, 2);
    printpreorder(root);
    return 0;
}

98.验证二叉搜索树 

// 验证二叉搜索树,中序遍历判断是否有序即可

#include <iostream>
#include <vector>

using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

void traversal(TreeNode *root, vector<int> &vec)
{
    if (root == NULL)
        return;
    traversal(root->left, vec);
    vec.push_back(root->val);
    traversal(root->right, vec);
}

bool isValidBST(TreeNode *root)
{
    vector<int> vec;
    traversal(root, vec);
    for (int i = 0; i < vec.size() - 1; i++)
    {
        if (vec[i] >= vec[i + 1])
            return false;
    }
    return true;
}

int main()
{
    TreeNode *t2 = new TreeNode(2);
    t2->left = new TreeNode(1);
    t2->right = new TreeNode(3);

    isValidBST(t2) ? cout << "true" << endl : cout << "false" << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值