【代码随想录】day20

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一、654最大二叉树

class Solution {
public:
    TreeNode* traversal(vector<int>& nums) {
        if (nums.size() == 0) return nullptr;
        int index = 0;
        int maxNum = 0;
        for (int i = 0; i < nums.size(); i ++) {
            if (nums[i] > maxNum) {
                maxNum = nums[i];
                index = i;
            }
        }
        TreeNode* root = new TreeNode(maxNum);
        vector<int> leftVec(nums.begin(), nums.begin() + index);
        vector<int> rightVec(nums.begin() + index + 1, nums.end());
        root->left = traversal(leftVec);
        root->right = traversal(rightVec);
        return root;
    }
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        return traversal(nums);
    }
};

优化版:

class Solution {
public:
    TreeNode* traversal(vector<int>& nums, int left, int right) {
        if (left == right) return nullptr;
        int index = left;
        int maxNum = 0;
        for (int i = left; i < right; i ++) {
            if (nums[i] > maxNum) {
                maxNum = nums[i];
                index = i;
            }
        }
        TreeNode* root = new TreeNode(maxNum);
        root->left = traversal(nums, left, index);
        root->right = traversal(nums, index + 1, right);
        return root;
    }
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        return traversal(nums, 0, nums.size());
    }
};

二、617合并二叉树

class Solution {
public:
    TreeNode* merge(TreeNode* node1, TreeNode* node2) {
        if (node1 == nullptr) {
            return node2;
        }
        if (node2 == nullptr) {
            return node1;
        }
        TreeNode* res = new TreeNode(node1->val + node2->val);
        res->left = merge(node1->left, node2->left);
        res->right = merge(node1->right, node2->right);
        return res;
    }
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
        if (root1 == nullptr) return root2;
        if (root2 == nullptr) return root1;
        return merge(root1, root2);
    }
};

三、700二叉搜索树中的搜索

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

优化版:

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

四、98验证二叉搜索树

class Solution {
public:
    bool isValid(TreeNode* node, long long low, long long high) {
        if (node == nullptr) return true;
        if (node->val <= low || node->val >= high) {
            return false;
        }
        return isValid(node->left, low, node->val) && isValid(node->right, node->val, high);

    }
    bool isValidBST(TreeNode* root) {
        return isValid(root, LONG_MIN, LONG_MAX);
    }
};

二叉搜索树的中序遍历是有序的!可以利用这个性质解题。
优化版:

class Solution {
public:
    long long minNum = LONG_MIN;
    bool isValid(TreeNode* node) {
        if (node == nullptr) {
            return true;
        }
        bool leftValid = isValid(node->left);
        if (node->val > minNum) {
            minNum = node->val;
        }
        else {
            return false;
        }
        bool rightValid = isValid(node->right);
        return leftValid && rightValid;
    }
    
    bool isValidBST(TreeNode* root) {
        return isValid(root);
    }
};

可以使用前后双指针,pre指向前一个结点比较有序性。再优化:

class Solution {
public:
    TreeNode* pre = nullptr;
    bool isValid(TreeNode* node) {
        if (node == nullptr) return true;
        bool leftValid = isValid(node->left);
        if (pre && node->val <= pre->val) {
            return false;
        }
        else {
            pre = node;
        }
        bool rightValid = isValid(node->right);
        return leftValid && rightValid;
    }

    bool isValidBST(TreeNode* root) {
        return isValid(root);
    }
};

总结

经过几天学习,写起二叉树还是很有思路的!(之前做题都是迷迷糊糊凭感觉写,纯看运气

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值