代码随想录打卡Day20| 二叉树part06

心得:凡是构造二叉树的题目,都是用前序遍历,因为要先构造中再处理左右子节点。

弄清楚定义数组时,nums.begin和nums.end指向的是什么。

注意:使用递归的时候,要注意这个递归函数返回的是什么,是可以直接用,还是要定义一个变量来保存这个结果。



第一题、最大二叉树 LeetCode654  https://leetcode.cn/problems/maximum-binary-tree/

需要先找到最大值并分割数组,再构造左右数组并进行递归。注意构造数组时的边界问题,否则会报错:

 注意注释中错误的部分

class Solution {
public:
    
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        TreeNode* node = new TreeNode(0);
        
        if(nums.size() == 1){
            node->val = nums[0];
            return node;
        }
        //找到数组中的最大值和对应下标
        int maxValue = 0;
        int index = 0;
        for(int i = 0; i < nums.size(); i++){
            if(maxValue < nums[i]){
                maxValue = nums[i];
                index = i;
            }
        }
        node->val = maxValue;

        if(index > 0){
            //错误1
            //new vector<int> newVec(nums.begin(), nums.begin() + index );
            vector<int> newVec(nums.begin(), nums.begin() + index );
            node->left = constructMaximumBinaryTree(newVec);
        } 

        //if(index < nums.size()){
            if(index < (nums.size() - 1)){
            //错误2:
            //new vector<int> newVec(nums.begin(), nums.begin() + index );
            //数组边界错误:
            //vector<int> newVec(nums.begin() + index , nums.end());
            vector<int> newVec(nums.begin() + index + 1 , nums.end());
            node->right = constructMaximumBinaryTree(newVec);
        } 

        return node;

    }
};


第二题、合并二叉树 LeetCode 617 https://leetcode.cn/problems/merge-two-binary-trees/

这道题要同时操作两个二叉树 ,但其实直接用root1然后把root2加上来就可以了。(当然也可以定义一个新的二叉树)。

注意,左右子树递归的部分不需要考虑空节点的情况,因为在上面已经讨论过了。

class Solution {
public:
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
        if(root1 == NULL) return root2;
        if(root2 == NULL) return root1;
        root1->val += root2->val;//中

        //if(root1->left) root1->left = mergeTrees(root1->left, root2->left);
        //if(root1->right) root1->right = mergeTrees(root1->right, root2->right);
        //这里其实并不需要判断,因为空节点在上面已经考虑过了
        root1->left = mergeTrees(root1->left, root2->left);//左
        root1->right = mergeTrees(root1->right, root2->right);//右

        return root1;
    }
};


第三题、二叉搜索树的搜索 LeeyCode700 https://leetcode.cn/problems/search-in-a-binary-search-tree/

注意在用递归的时候,要看这个函数返回的是什么,加入返回了一棵根节点,则需要新建一个节点来接住这个结果,否则就会报错。

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

        TreeNode* res = NULL;

        if(root->val > val){
            //res = root->left;
            res = searchBST(root->left, val);
        }
        if(root->val < val){
            //res = root->right;
            res = searchBST(root->right, val);
        }

        if(root->val == val) res = root;

        return res;

    }
};

 迭代法:

本题用迭代法也很简单,但要注意再while循环中没找都,要再循环外return NULL

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

        //迭代法
        while(root != NULL){
            if(root->val > val) root = root->left;
            else if(root->val < val) root = root->right;
            else return root;
        }

        return NULL;

        

    }
};


第四题、验证二叉搜索树 LeetCode98 https://leetcode.cn/problems/validate-binary-search-tree/

首先可以用中序遍历,把所有节点放到一个数组中,然后看这个数组是不是单调递增就可以。

 注意,判断时用相减是否大于0容易出错

class Solution {
public:
    long long maxValue = LONG_MIN;
    vector<int> vec;
    void traversal(TreeNode* node){
        if(node == NULL) return;
        traversal(node->left);
        vec.push_back(node->val);
        traversal(node->right);
    }

    bool isValidBST(TreeNode* root) {
        if(root == NULL) return true;
        vec.clear();
        traversal(root);
        for(int i = 1; i < vec.size(); i++){
            // if(vec[i] - vec[i - 1] <= 0) return false;
            if(vec[i]  <= vec[i - 1]) return false;
        }
        return true;
    }
};

这里用一个不使用额外数组的方法,直接判断节点是否是搜索树。 

关键在于用maxValue来一直记录二叉树的值,若maxValue没有一个更新,说明中序遍历不是递增的,所以就return false。

class Solution {
public:
    long long maxValue = LONG_MIN;
    bool isValidBST(TreeNode* root) {
        if(root == NULL) return true;
        
        bool left = isValidBST(root->left);

        if(maxValue < root->val){
            maxValue = root->val;
        }
        else return false;

        bool right = isValidBST(root->right);

        return left && right;
    }
};

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值