随想录二刷Day20——二叉树

二叉树

19. 最大二叉树

654. 最大二叉树

思路:
每次找到最大值所在的下标,利用先序遍历的方法,递归构造二叉树

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

private:
    TreeNode *preorder(vector<int> &nums, int start, int end) { // [start, end)
        if (end - start <= 0) return nullptr;
        int max = start; // 存最大值的下标
        for (int i = start + 1; i < end; i++) {
            if (nums[i] > nums[max]) max = i;
        }
        TreeNode *root = new TreeNode(nums[max]);
        root->left = preorder(nums, start, max);
        root->right = preorder(nums, max + 1, end);
        return root;
    }
};

21. 合并二叉树

617. 合并二叉树

思路:
对两棵子树同步遍历

  1. 遇到一个为空,则返回另一个
  2. 都不为空则相加
  3. 都为空,则返回空
class Solution {
public:
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
        if (root1 == nullptr && root2 == nullptr) return nullptr;
        if (root1 == nullptr && root2 != nullptr) {
            return root2;
        }
        if (root1 != nullptr && 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;
    }
};

迭代法

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);

        while (!que.empty()) {
            TreeNode *cur1 = que.front(); que.pop();
            TreeNode *cur2 = que.front(); que.pop();
            cur1->val += cur2->val;

            if (cur1->left && cur2->left) {
                que.push(cur1->left);
                que.push(cur2->left);
            }

            if (cur1->right && cur2->right) {
                que.push(cur1->right);
                que.push(cur2->right);
            }

            if (!cur1->left && cur2->left) {
                cur1->left = cur2->left;
            }
            if (!cur1->right && cur2->right) {
                cur1->right = cur2->right;
            }
        }

        return root1;
    }
};

22. 二叉搜索树中的搜索

700. 二叉搜索树中的搜索

思路:
二分查找

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

迭代法

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

23. 验证二叉搜索树

98. 验证二叉搜索树

思路:
递归的要求左子树的所有节点值小于根节点,所有右子树节点值大于根节点
看下面这个样例中,右子树中存在小于左子树的值,所以不满足。在这里插入图片描述
二叉搜索树的中序遍历结果应该是一个递增序列。

class Solution {
public:
    bool isValidBST(TreeNode* root) {
        vector<int> orders;
        midorder(root, orders);
        for (int i = 1; i < orders.size(); i++) {
            if (orders[i - 1] >= orders[i]) return false;
        }
        return true;
    }

private:
    void midorder(TreeNode *root, vector<int>& orders) {
        if (root == nullptr) return ;

        midorder(root->left, orders);
        orders.push_back(root->val);
        midorder(root->right, orders); 
    }
};

优化: 直接在遍历过程中,判断遍历结果是否递增。

class Solution {
public:
    TreeNode *pre = nullptr;
    bool isValidBST(TreeNode* root) {
        if (root == nullptr) return true;
        bool left = isValidBST(root->left); // 左
        if (pre != NULL && pre->val >= root->val) return false; // 中
        pre = root; // 记录前一个节点
        bool right = isValidBST(root->right); // 右
        return left && right;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值