当前位置: 代码迷 >> 综合 >> Leetcode 1110. 删点成林(DAY 12)
  详细解决方案

Leetcode 1110. 删点成林(DAY 12)

热度:15   发布时间:2023-11-17 20:31:36.0

原题题目

在这里插入图片描述



代码实现(首刷半看解半自解)

/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*//*** Note: The returned array must be malloced, assume caller calls free().*/#define MAX 10001bool whetherdelect(int x,int to_deleteSize,int* to_delete)
{
    int i;for(i=0;i<to_deleteSize;i++){
    if(to_delete[i] == x)return true;}return false;
}struct TreeNode* visit(struct TreeNode* root,int* to_delete,int to_deleteSize,int* returnSize,struct TreeNode** ret)
{
    if(root){
    root->left = visit(root->left,to_delete,to_deleteSize,returnSize,ret);root->right = visit(root->right,to_delete,to_deleteSize,returnSize,ret);if(true == whetherdelect(root->val,to_deleteSize,to_delete)){
    if(root->left)ret[(*returnSize)++] = root->left;if(root->right)ret[(*returnSize)++] = root->right;root = NULL;}return root;}return NULL;
}struct TreeNode** delNodes(struct TreeNode* root, int* to_delete, int to_deleteSize, int* returnSize){
    *returnSize = 0;struct TreeNode** ret = (struct TreeNode **)malloc(sizeof(struct TreeNode*) * MAX);root = visit(root,to_delete,to_deleteSize,returnSize,ret);if(root)ret[(*returnSize)++] = root;return ret;
}