找工作准备刷题Day6 二叉树 (卡尔41期训练营 7.19)

第一题:Leetcode654. 最大二叉树

题目描述

题解

class Solution {
public:
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        return CMBTUtil(nums, 0, nums.size());
    }

    TreeNode* CMBTUtil(const vector<int>& nums, int left, int right) {
        if (left >= right)
            return nullptr;

        int max_value = nums[left];
        int max_idx = left;
        for (int i = left + 1; i < right; i++) {
            if (nums[i] > max_value) {
                max_value = nums[i];
                max_idx = i;
            }
        }

        TreeNode* root = new TreeNode(max_value);
        root->left = CMBTUtil(nums, left, max_idx);
        root->right = CMBTUtil(nums, max_idx + 1, right);
        return root;
    }
};

要点

  1. 通过数组左右下标来表示递归区域,避免数组的重复创建和销毁。
  2. 牢记左闭右开的特点。

第二题:Leetcode617.合并二叉树

题目描述

题解

class Solution {
public:
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
        if (root1 == nullptr)
            return root2;
        if (root2 == nullptr)
            return root1;

        queue<TreeNode*> que;
        que.push(root1);
        que.push(root2);
        TreeNode *node1, *node2;
        while (!que.empty()) {
            node1 = que.front(), que.pop();
            node2 = que.front(), que.pop();

            node1->val += node2->val;

            if (node1->left != nullptr && node2->left != nullptr) {
                que.push(node1->left);
                que.push(node2->left);
            }
            if (node1->right != nullptr && node2->right != nullptr) {
                que.push(node1->right);
                que.push(node2->right);
            }

            if (node1->left == nullptr && node2->left != nullptr) {
                node1->left = node2->left;
            }
            if (node1->right == nullptr && node2->right != nullptr) {
                node1->right = node2->right;
            }
        }

        return root1;
    }
};

诀窍

  1. 当某一个为空,直接返回另一个即可。
  2. 如何保证不使用旧的元素?从而避免提前释放?

第三题:Leetcode200.二叉树中的搜索

题目描述

题解

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

要点

  1. 主要搜索范围,不要进错分支

第四题:Leetcode98.验证二叉搜索树

题目描述

思路

  1. 并不能通过比较节点的左<根<右来判断,因为要求是根>所有左子树的节点
  2. 可以通过中序遍历,比较根节点和左子树最后一个的节点来判断是否满足要求。

题解1——递归方法

class Solution {
public:
    TreeNode* pre = nullptr;
    bool isValidBST(TreeNode* root) {
        if (root == nullptr)
            return true;
        bool bLeft = isValidBST(root->left);
        // 注意是pre->val >= root->val 左子树必须小于根
        if (pre != nullptr && pre->val >= root->val)
            return false;

        pre = root;

        bool bRight = isValidBST(root->right);
        return bLeft && bRight;
    }
};

需要注意 大于等于(>=)。


题解2——栈迭代法

class Solution {
public:
    bool isValidBST(TreeNode* root) {
        stack<TreeNode*> mystack;
        TreeNode *cur = root, *pre = nullptr;
        while (cur != nullptr || !mystack.empty()) {
            if (cur != nullptr) {
                mystack.push(cur);
                cur = cur->left;
            } else {
                cur = mystack.top(), mystack.pop();
                if (pre != nullptr && pre->val >= cur->val)
                    return false;
                pre = cur;
                cur = cur->right;
            }
        }
        return true;
    }
};

似乎无法使用统一迭代法

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值