day20.|二叉树06

本文介绍了如何在LeetCode中实现最大二叉树的构造、二叉树的合并、搜索以及验证二叉搜索树的方法,重点涉及前序遍历、区间分割、迭代和中序遍历的应用。

1.最大二叉树

. - 力扣(LeetCode)

思路:

1.构造二叉树类的题目 均要用到 前序遍历!!!因为先构造中间节点,然后递归构造左子树和右子树。

2.分割区间时,一定要确保是左闭右开 还是 左闭右闭,(本题是左闭右开)。

class Solution {
public:
    TreeNode* construct(vector<int>& nums){
        if(nums.size() == 0)  return NULL;

        int Maxvalue = 0;
        int index = 0;
        for(int i = 0; i < nums.size(); i++){
            if(nums[i] > Maxvalue){
                Maxvalue = nums[i];
                index = i;
            }
        }
        
        //前序
        TreeNode* root = new TreeNode(Maxvalue);

        vector<int> leftnums(nums.begin(), nums.begin() + index);
        root -> left = construct(leftnums);

        vector<int> rightnums(nums.begin() + index + 1, nums.end());
        root -> right = construct(rightnums);

        return root;
    }

    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        // if(nums.size() == 0)  return NULL;
        return construct(nums);
    }
};

3.进行左右区间分割的时候,需不需要if判断,主要在于终止条件的判断。


 2.合并二叉树

. - 力扣(LeetCode)

思路:

1.一起操作两个二叉树。这种操作在之前 二叉树的对称 的题中遇见过。

2.遍历顺序: 前序遍历

class Solution {
public:
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
        if(root1 == NULL) return root2;
        if(root2 == NULL) return root1;

        root1 -> val += root2 -> val;
        root1 -> left = mergeTrees(root1 -> left,root2 -> left);
        root1 -> right = mergeTrees(root1 -> right,root2 -> right);

        return root1;
    }
};

3.二叉搜索树中的搜索 

. - 力扣(LeetCode)

思路:

1.二叉搜索树明确了搜索的方向,利用迭代法也较为简单。

2.二叉搜索树不用再强调遍历顺序(前中后序),因为二叉搜索树自带顺序,确定了我们的搜索方向。

3.二叉搜索树是一个有序树。

class Solution {
public:
    TreeNode* searchBST(TreeNode* root, int val) {
        //递归法
        if(root == NULL || root -> val == val)  return root;

        TreeNode* result = NULL;
        if(root -> val > val)
        result = searchBST(root -> left, val);

        if(root -> val < val)
        result = searchBST(root -> right,val);

        return result; 
    }
};

 4. 对于二叉搜索树,不需要回溯的过程,因为节点的有序性就帮我们确定了搜索的方向。

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

4.验证二叉搜索树

. - 力扣(LeetCode)

思路:

1.遇到二叉搜索树时,或者判断二叉树是否为二叉搜索树时,都要想到中序遍历。因为中序遍历下的二叉搜索树是一个有序数组。

class Solution {
public:
    void traversal(TreeNode* cur, vector<int>& nums){
        if(cur == NULL) return ;
        
        //将二叉搜索树转换为有序数组
        //中序
        traversal(cur -> left, nums);
        nums.push_back(cur -> val);
        traversal(cur -> right,nums);
        return;
    }

    bool isValidBST(TreeNode* root) {
        if(root == NULL) return true;
        vector<int> nums;
        traversal(root,nums);
        for(int i = 1; i < nums.size(); i++){
            if(nums[i] <= nums[i - 1]) 
            return false;
        }
        return true;
    }
};
class Solution {
public:
    TreeNode* pre = NULL;
    bool isValidBST(TreeNode* root) {
        if(root == NULL) 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;
    }
};

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值