【二叉树6】654.最大二叉树 617.合并二叉树 700.二叉搜索树中的搜索 98.验证二叉搜索树

654.最大二叉树

题目链接

(1)文字讲解:https://programmercarl.com/0654.最大二叉树.html
(2)视频讲解:
(3)题目链接:https://leetcode.cn/problems/maximum-binary-tree/

看到题解之前的想法

和用中序和前序建立二叉树的思想类似

看到题解之后的想法

🈚️

本题难点

🈚️

代码

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* constructTree(vector<int> nums, int start, int end){
        if(end-start==0){
            TreeNode* node = new TreeNode(nums[start]);
            return node;
        }
        int maxnum = 0;
        int maxind = 0;
        for(int i = start; i <= end; i++){
            if(nums[i] >= maxnum){
                maxnum = nums[i];
                maxind = i;
            }
        }
        TreeNode* node = new TreeNode(nums[maxind]);
        if(maxind > start){
            node->left = constructTree(nums, start, maxind-1);
        }
        if(maxind < end){
            node->right = constructTree(nums, maxind+1, end);
        }
        return node;
    } 
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        return constructTree(nums, 0, nums.size()-1);
    }
};

617.合并二叉树

题目链接

(1)文字讲解:https://programmercarl.com/0617.合并二叉树.html#思路
(2)视频讲解:
(3)题目链接:https://leetcode.cn/problems/maximum-binary-tree/

看到题解之前的想法

🈚️

看到题解之后的想法

同时操作两棵二叉树,要点就是将两棵同时传入函数中。和之前的二叉树对称是一样的思想,一样的位置,那就相加,某棵树为空,那就直接复制非空的那一棵。其中,递归法和迭代法都能用,一个是深度优先搜索一个是层序遍历就是了。

本题难点

同时操作两棵二叉树,要点就是将两棵同时传入函数中

代码

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
        if(root1 == NULL && root2 == NULL) return root1;
        if(root1 == NULL && root2 != NULL) return root2;
        if(root1 != NULL && root2 == NULL) return root1;
        root1->val += root2->val;
        root1->right = mergeTrees(root1->right, root2->right);
        root1->left = mergeTrees(root1->left, root2->left);
        return root1;
    }
};
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
        if(root1 == NULL && root2 == NULL) return root1;
        if(root1 == NULL && root2 != NULL) return root2;
        if(root1 != NULL && root2 == NULL) return root1;
        queue<TreeNode*> que;
        que.push(root1);
        que.push(root2);
        while(!que.empty()){
            TreeNode* t1 = que.front();
            que.pop();
            TreeNode* t2 = que.front();
            que.pop();
            t1->val += t2->val;
            if(t1->left != NULL && t2->left != NULL){
                que.push(t1->left);
                que.push(t2->left);
            }
            if(t1->right != NULL && t2->right != NULL){
                que.push(t1->right);
                que.push(t2->right);
            }
            if(t1->left == NULL && t2->left != NULL){
                t1->left = t2->left;
            }
            if(t1->right == NULL && t2->right != NULL){
                t1->right = t2->right;
            }
        }
        return root1;
    }
};

700.二叉搜索树中的搜索

题目链接

(1)文字讲解:https://programmercarl.com/0700.二叉搜索树中的搜索.html#思路
(2)视频讲解:
(3)题目链接:https://leetcode.cn/problems/search-in-a-binary-search-tree/

看到题解之前的想法

🈚️

看到题解之后的想法

利用二叉搜索树的特性,及左子树的值都小于根节点,右子树的值都大于根节点,然后比对val和root-》val的大小来确定接下来往左边还是右边走。

本题难点

利用二叉搜索树的特性,及左子树的值都小于根节点,右子树的值都大于根节点

代码

递归法

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* searchBST(TreeNode* root, int val) {
        if(root == NULL || root->val == val) return root;
        TreeNode* result = NULL;
        if(val > root->val) result = searchBST(root->right, val);
        if(val < root->val) result = searchBST(root->left, val);
        return result;
        }
};

迭代法

 /**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* searchBST(TreeNode* root, int val) {
        while(root){
            if(val > root->val) root = root->right;
            else if(val < root->val) root = root->left;
            else break;
        }
        return root;
    }
};

98.验证二叉搜索树

题目链接

(1)文字讲解:https://programmercarl.com/0098.验证二叉搜索树.html#思路
(2)视频讲解:
(3)题目链接:https://leetcode.cn/problems/validate-binary-search-tree/description/

看到题解之前的想法

🈚️

看到题解之后的想法

可以选择直接记录中序遍历结果,因为合格的二叉搜索树的中序遍历结果是递增的数组(严格递增,二叉搜索树不存在相等的节点)
也可以选择在中序遍历过程中直接检查,但是就是要记录一下上一个遍历的节点的值!
中序遍历的写法就随便挑一个就好。

本题难点

合格的二叉搜索树的中序遍历结果是递增的数组(严格递增,二叉搜索树不存在相等的节点)

代码

1.中序遍历,然后检查是否为递增序列

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void traversal(TreeNode* node, vector<int>& vec){
        if(node->left) traversal(node->left, vec);
        vec.push_back(node->val);
        if(node->right) traversal(node->right, vec);
    }
    bool isValidBST(TreeNode* root) {
        vector<int> res;
        traversal(root, res);
        for(int i = 1; i < res.size(); i++){
            if(res[i-1] >= res[i]) return false;
        }
        return true;
    }
};

2.直接中序遍历中检查是否一直递增

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
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;
    }
};

3.迭代法中序遍历(前中后不统一写法)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isValidBST(TreeNode* root) {
        stack<TreeNode*> st;
        TreeNode* cur = root;
        TreeNode* pre = NULL;
        while(cur != NULL || !st.empty()){
            if(cur != NULL){
                st.push(cur);
                cur = cur->left;
            }else{
                cur = st.top();
                st.pop();
                if(pre != NULL && pre->val >= cur->val) return false;
                pre = cur;
                cur = cur->right;
            }
        }
        return true;
    }
};

4.迭代法中序遍历(前中后统一)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isValidBST(TreeNode* root) {
        stack<TreeNode*> st;
        TreeNode* pre = NULL;
        st.push(root);
        while(!st.empty()){
            TreeNode* cur = st.top();
            st.pop();
            if(cur != NULL){
                if(cur->right) st.push(cur->right);
                st.push(cur);
                st.push(NULL);
                if(cur->left) st.push(cur->left); 
            }else{
                cur = st.top();
                st.pop();
                if(pre != NULL && pre->val >= cur->val) return false;
                pre = cur;
            }
        }
        return true;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值