Day17 | LeetCode 654. 最大二叉树, 617. 合并二叉树, 700. 二叉搜索树中的搜索, 98. 验证二叉搜索树

本文介绍了LeetCode中的四个与二叉树相关的问题,包括最大二叉树的构造、两棵树的合并、在二叉搜索树中的搜索以及验证二叉搜索树的递归和迭代解决方案。

LeetCode 654. 最大二叉树

class Solution {
public:
    TreeNode* traversal(vector<int>& nums, int start, int end) {
        if (start == end) {
            return nullptr;
        }
        int maxIndex = start, maxValue = nums[maxIndex];
        for (int i = start; i < end; i++) {
            if (nums[i] > nums[maxIndex]) {
                maxIndex = i;
                maxValue = nums[maxIndex];
            }
        }
        TreeNode* root = new TreeNode(maxValue);
        root->left = traversal(nums, start, maxIndex);
        root->right = traversal(nums,maxIndex + 1, end);
        return root;
    }

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

};

LeetCode 617. 合并二叉树

递归
class Solution {
public:
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
        if (!root1) {
            return root2;
        }
        if (!root2) {
            return root1;
        }
        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* node1;
        TreeNode* node2;
        while (!que.empty()) {
            node1 = que.front();
            que.pop();
            node2 = que.front();
            que.pop();
            node1->val += node2->val;
            if (node1->left && node2->left) {
                que.push(node1->left);
                que.push(node2->left);
            }
            if (node1->right && node2->right) {
                que.push(node1->right);
                que.push(node2->right);
            }
            if (!node1->left && node2->left) {
                node1->left = node2->left;
            }
            if (!node1->right && node2->right) {
                node1->right = node2->right;
            }

        }
        return root1;
    }
};

LeetCode 700. 二叉搜索树中的搜索

递归
class Solution {
public:
    TreeNode* searchBST(TreeNode* root, int val) {
        if (!root || root->val == val){
            return root;
        }
        if (root->val > val){  
            return searchBST(root->left, val);
        } else {
            return searchBST(root->right, val);
        }
    }
};
迭代
class Solution {
public:
    TreeNode* searchBST(TreeNode* root, int val) {
        while (root) {
            if (root->val == val) {
                return root;
            }else if (root->val > val) {
                root = root->left;
            }else {
                root = root->right;
            }
        }
        return nullptr;
    }
};

LeetCode 98. 验证二叉搜索树

class Solution {
public:
    long pre = LONG_MIN;
    bool isValidBST(TreeNode* root) {
        if (!root) {
            return true;
        }
        //中序遍历
        if (!isValidBST(root->left)) {
            return false;
        }
        if (pre >= root->val) {
            return false;
        }else {
            pre = root->val;
        }
        return isValidBST(root->right);
    }
};
中序遍历(迭代)
class Solution {
public:
    bool isValidBST(TreeNode* root) {
        if (!root) {
            return true;
        }
        stack<TreeNode*> sta;
        TreeNode* pre = nullptr;
        while (!sta.empty() || root) {
            if (root) {
                sta.push(root);
                root = root->left;
            }else {
                root = sta.top();
                sta.pop();
                if (pre && pre->val >= root->val) {
                    return false;
                }
                pre = root;
                root = root->right;
            }
        }
        return true;
    }
};
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值