代码随想录第十七天(一刷&&C语言)|最大二叉树&&合并二叉树&&二叉搜索树中的搜索&&验证二叉搜索树

创作目的:为了方便自己后续复习重点,以及养成写博客的习惯。

一、最大二叉树

思路:构造树采用前序遍历,先构造中间节点,再递归构造左子树和右子树。

ledcode题目:https://leetcode.cn/problems/maximum-binary-tree/

AC代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* construct(const int* nums,int left,int right) {
    if(left > right) {
        return NULL;
    }
    int best = left;
    for(int i = left + 1;i <= right;++i) {
        if(nums[i] > nums[best]) {
            best = i;
        }
    }
    struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    node->val = nums[best];
    node->left = construct(nums,left,best - 1);
    node->right = construct(nums,best + 1,right);
    return node;
}
struct TreeNode* constructMaximumBinaryTree(int* nums, int numsSize) {
    return construct(nums,0,numsSize - 1);
}

二、合并二叉树

思路:两个节点重叠,值相加作为节点合并后的新值,但还要考虑到没有节点的情况,再分别对左右子树进行递归。

lecode题目:https://leetcode.cn/problems/merge-two-binary-trees/

AC代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* mergeTrees(struct TreeNode* root1, struct TreeNode* root2) {
    if(root1 == NULL) {
        return root2;
    }
    if(root2 == NULL) {
        return root1;
    }
    struct TreeNode* node = malloc(sizeof(struct TreeNode));
    node->val = root1->val + root2->val;
    node->left = mergeTrees(root1->left,root2->left);
    node->right = mergeTrees(root1->right,root2->right);
    return node;
}

三、二叉搜索树中的搜索

思路:在BST中找到root->val等于val的节点。 返回以该节点为根的子树。若节点不存在,则返回 NULL。

ledcode题目:https://leetcode.cn/problems/search-in-a-binary-search-tree/description/

AC代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* searchBST(struct TreeNode* root, int val) {
    if(root == NULL) {
        return NULL;
    }
    if(root->val == val) {
        return root;
    }else if(root->val > val) {
        return searchBST(root->left,val);
    }
    return searchBST(root->right,val);

}

 四、验证二叉搜索树

思路:中序遍历下输出的二叉搜索树节点的数值是有序序列,验证二叉搜索树就转换为判断是不是递增。

ledcode题目:https://leetcode.cn/problems/validate-binary-search-tree/description/

AC代码:

以下两个解法参考carl的思路,测试均能过,但后续提交解答错误,留给自己之后有时间做思考。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

bool isValidBST(struct TreeNode* root) {
   long long maxVal = LONG_MIN;
   if(root == NULL) {
       return true;
   }
   bool left = isValidBST(root->left);
  // 中序遍历,验证遍历的元素是不是从小到大
   if(maxVal < root->val) {
       maxVal = root->val;
   }else {
       return false;
   }
   bool right = isValidBST(root->right);
   return left && right;
}
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* pre = NULL;     
bool isValidBST(struct 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;
}

全篇后记:

加油,坚持就是胜利。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值