代码随想录第17天|leetcode654.最大二叉树、leetcode617.合并二叉树、leetcode700.二叉搜索树中的搜索、leetcode98.验证二叉搜索树

1.654. 最大二叉树 - 力扣(LeetCode)

思路:类似于昨天的“前序+中序”确定一颗二叉树&&“后序+中序”确定一颗二叉树,按着六步走。

    TreeNode *constructMaximumBinaryTree(vector<int> &nums)
    {
        if (nums.empty())
            return nullptr;
        int ind = 0;
        int max = nums[0];
        TreeNode *root = new TreeNode();
        for (int i = 1; i < nums.size(); i++)
        {
            if (nums[i] > max)
            {
                max = nums[i];
                ind = i; // 找到最大值的下标
            }
        }
        root->val = nums[ind];
        nums.erase(nums.begin() + ind);
        vector<int> left(nums.begin(), nums.begin() + ind);
        vector<int> right(nums.begin() + ind, nums.end());
        root->left = constructMaximumBinaryTree(left);
        root->right = constructMaximumBinaryTree(right);
        return root;
    }

但是时间和空间复杂度较高,用数组构造二叉树的题目,每次分隔尽量不要定义新的数组,而是通过下标索引直接在原数组上操作,这样可以节约时间和空间上的开销。如果让空节点(空指针)进入递归,就不加if,如果不让空节点进入递归,就加if限制一下, 终止条件也会相应的调整。如下:

    TreeNode *construct(vector<int> &nums, int left, int right)
    {
        if (left >= right)
            return nullptr;
        int max_value = left; // 分割点下标
        for (int i = left + 1; i < right; i++)
        {
            if (nums[i] > nums[max_value])
                max_value = i;
        }
        TreeNode *root = new TreeNode(nums[max_value]);
        root->left = construct(nums, left, max_value);
        root->right = construct(nums, max_value + 1, right);
        return root;
    }
    TreeNode *constructMaximumBinaryTree(vector<int> &nums)
    {
        return construct(nums, 0, nums.size());
    }

2.617. 合并二叉树 - 力扣(LeetCode)

思路:这道题超级简单

    TreeNode *mergeTrees(TreeNode *root1, TreeNode *root2)
    {
        if (root1 == nullptr)
            return root2;
        if (root2 == nullptr)
            return root1;
        root1->val = root1->val + root2->val;
        root1->left = mergeTrees(root1->left, root2->left);
        root1->right = mergeTrees(root1->right, root2->right);
        return root1;
    }

3.700. 二叉搜索树中的搜索 - 力扣(LeetCode)

思路1:层次遍历法。最开始想开一个vector,然后把结果存入的。但你后来发现,node其实相当于头节点返回node就可以返回他下面所有的孩子节点了。下面的做法属于没读题...(人家都说了在二叉搜索树里...)

     TreeNode* searchBST(TreeNode* root, int val) {
        if(root == nullptr){
            return nullptr;
        }
        queue<TreeNode*> que;
        que.push(root);
        while(!que.empty()){
            TreeNode *node = que.front();
            que.pop();
            if(node->val == val){
                return node;
            }else{
                if(node->left){
                    que.push(node->left);
                }
                if(node->right){
                    que.push(node->right);
                }
            }
        }
        return nullptr;
    }

思路2:迭代法。

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

思路3:递归法

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

4.98. 验证二叉搜索树 - 力扣(LeetCode)

思路:这道题第一次写的时候,没有观察到这其实就是中序遍历结果,并判断是不是单调递增的....

    void inorder(TreeNode *root,vector<int>&res){
        if(root == nullptr)return;
        inorder(root->left, res);
        res.push_back(root->val);
        inorder(root->right, res);
    }
    bool isValidBST(TreeNode *root)
    {
        if (root == nullptr)
            return true;
        vector<int> res;
        inorder(root, res);
        for (int i = 0; i < res.size() - 1; i++){
            if(res[i] >= res[i + 1])return false;
        }
        return true;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值