代码随想录算法训练营第二十天|654.最大二叉树 617.合并二叉树 700.二叉搜索树中的搜索 98.验证二叉搜索树

LeetCode 654.最大二叉树

题目链接:654.最大二叉树

踩坑:跟前天的由中序后序遍历序列构造二叉树类似,所以没踩坑。

思路:跟中序后序遍历序列构造二叉树一样,停止条件就是何时这棵树简单到可以直接返回就返回。其他时间都是分割数组,将左子树和右子树继续向下递归。

代码:

/**
 * 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* constructMaximumBinaryTree(vector<int>& nums) {
        if(nums.empty()) return nullptr;
        int max = INT_MIN;
        int maxindex = -1;
        for(int i = 0; i < nums.size(); i++)
        {
            if(nums[i] > max)
            {
                max = nums[i];
                maxindex = i;
            }
        }
        TreeNode* root = new TreeNode(max);
        if(nums.size() == 1) return root;

        vector<int> leftsubtree(nums.begin(), nums.begin()+maxindex);
        vector<int> rightsubtree(nums.begin()+maxindex+1, nums.end());

        TreeNode* leftroot = constructMaximumBinaryTree(leftsubtree);
        TreeNode* rightroot = constructMaximumBinaryTree(rightsubtree);

        root->left = leftroot;
        root->right = rightroot;

        return root;
    }
};

LeetCode 617.合并二叉树

题目链接:617.合并二叉树

踩坑:在犹豫到底需不需要保护原结构,意识到了两棵树采用同样的遍历顺序是同步的,但是没有想到终止条件应该是什么,直接返回另一棵树这种操作还是挺启发式的。

思路:如果一棵树遍历到null了,直接返回另一棵树的当前节点进行补缺(非常的简单粗暴)。其实也很好想,一颗空树和另一颗树合并,肯定是直接返回非空树就好了。不需要考虑深浅拷贝的问题。

代码:

/**
 * 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 == nullptr) return root2;
        if(root2 == nullptr) return root1;

        root1->val += root2->val;

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

        return root1;
    }
};

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

题目链接:700.二叉搜索树中的搜索

踩坑:比较简单,只需要熟悉掌握一下二叉搜索树的概念即可。

思路:

  1. 递归:二叉搜索树(左小右大),向下递归,找到等值节点则返回,如果是null也返回。
  2. 迭代:向下迭代,左小右大,找到等值break,为空退出。

代码:

/**
 * 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 == nullptr || root->val == val) return root;

        TreeNode* result = nullptr;
        if(root->val < val) result = searchBST(root->right, val);
        else if(root->val > val) result = searchBST(root->left, val);

        return result;
    }
};

LeetCode 98.验证二叉搜索树

题目链接:98.二叉搜索树

踩坑:二叉搜索树左子树的所有节点都要小于根节点,右子树所有节点都要大于根节点,并不是只针对二叉搜索树的根节点就能构建二叉搜索树。

思路:二叉搜索树如果按照中序遍历会得到一个严格的升序的序列。为了达到这种判断,可以使用一个全局变量来记录处理过的最大值,也可以使用一个全局指针来记录前一个节点。

代码:

/**
 * 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 = nullptr;
    bool isValidBST(TreeNode* root) {
        if(!root) return true;

        bool left = isValidBST(root->left);
        if(pre && pre->val >= root->val) return false;
        pre = root;
        bool right = isValidBST(root->right);
        return left && right;
    }
};
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值