算法刷题Day 20 最大二叉树+合并二叉树+二叉搜索树中的搜索+验证二叉搜索树

Day 20 二叉树

654. 最大二叉树

递归

class Solution {
    TreeNode *build(const vector<int> &nums, int left, int right)
    {
        if (left >= right) return nullptr;
        int idx = left;
        for (int i = left + 1; i < right; ++i) 
        {
            if (nums[i] > nums[idx])
            {
                idx = i;
            }
        }
        TreeNode *root = new TreeNode(nums[idx]);
        root->left = build(nums, left, idx);
        root->right = build(nums, idx + 1, right);
        return root;
    }

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

617. 合并二叉树

递归

class Solution {
public:
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
        if (!root1 && !root2) return nullptr;
        else if (!root1) return root2;
        else if (!root2) 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) return root2;
        if (!root2) return root1;

        queue<TreeNode*> que;

        que.push(root1);
        que.push(root2);

        TreeNode *cur1, *cur2;
        while (!que.empty())
        {
            cur1 = que.front();
            que.pop();
            cur2 = que.front();
            que.pop();

            cur1->val = 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;
    }
};

700. 二叉搜索树中的搜索

递归

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

迭代

class Solution {
public:
    TreeNode* searchBST(TreeNode* root, int val) {
        TreeNode *cur = root;

        while (cur)
        {
            if (cur->val > val)
            {
                cur = cur->left;
            }
            else if (cur->val < val)
            {
                cur = cur->right;
            }
            else
            {
                return cur;
            }
        }

        return nullptr;
    }
};

98. 验证二叉搜索树

遇到两个坑的地方:

  1. 递归的时候,不能光验证左子树的根节点小于当前根节点,右子树的根节点大于但当前根节点,别忘了左子树上的每一个节点都要求比根节点要小,右子树上的每一个节点都要求比根节点大。
  2. 根节点跟左(右)子树中的某一个节点相等,也是不符合二叉搜索树的规则的

递归

用记录前一个节点的方法来处理

class Solution {    
    TreeNode *pre = nullptr;
    bool flag = true;

    void tracking(TreeNode *root)
    {
        if (!root) return;
        tracking(root->left);
        if (pre)
        {
            if (root->val <= pre->val) // 注意:这里是等号,两值相等也不符合
            {
                flag = false;
            }
        }
        pre = root;
        tracking(root->right);
    }

public:
    bool isValidBST(TreeNode* root) {
        tracking(root);
        return flag;
    }
};

迭代

class Solution {
public:
    bool isValidBST(TreeNode* root) {
        stack<TreeNode*> stk;
        vector<int> table;

        TreeNode *cur = root;
        while (cur || !stk.empty())
        {
            if (cur)
            {
                stk.push(cur);
                cur = cur->left;
            }
            else
            {
                cur = stk.top();
                stk.pop();
                table.push_back(cur->val);
                cur = cur->right;
            }
        }

        for (int i = 1; i < table.size(); ++i)
        {
            if (table[i] <= table[i - 1])
            {
                return false;
            }
        }

        return true;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值