C数据结构-树相关的递归

leetcode863题,二叉树中所有距离为 K 的结点。给定一个二叉树(具有根结点 root), 一个目标结点 target ,和一个整数值 K 。返回到目标结点 target 距离为 K 的所有结点的值的列表。 答案可以以任何顺序返回。
1、给一个DFS函数,来表示这棵树是否有target,以及他们的距离
2、如果刚刚等于target,就反馈出来下面对应距离的节点;
3、如果左边有target,那么就看右边的距离
4、如果右边有target,那么就看左边的距离

int g_count;
int GetNodeNum(struct TreeNode* root)
{
    
    
    if (root==NULL) {
    
    
        return 0;
    }
    return GetNodeNum(root->left) + GetNodeNum(root->right) + 1;
}

void ADD(struct TreeNode* root, int depth, int *ret)
{
    
       
    if(depth < 0) {
    
    
        return;
    }
    if(root == NULL){
    
    
        return;
    }
    if(depth == 0) {
    
    
        ret[g_count++] = root->val;
    }
    ADD(root->left, depth-1, ret);
    ADD(root->right, depth-1, ret);
}
 
int DFS(struct TreeNode* root, struct TreeNode* target, int depth, int *ret)
{
    
    
    if(root == NULL) {
    
    
        return -1;
    }
    if(root == target) {
    
    
        ADD(target, depth, ret);
        return 0;
    }else{
    
    
        int tmpl, tmpr;//看下左右子树跟target的距离
        tmpl = DFS(root->left, target, depth, ret);
        tmpr = DFS(root->right, target, depth, ret);
        if(tmpl != -1) {
    
    
            if(tmpl+1==depth) {
    
    
                ret[g_count++] = root->val;
            }
            ADD(root->right, depth-tmpl-2, ret);
            return tmpl+1;
        }
        if(tmpr != -1) {
    
    
            if(tmpr+1==depth) {
    
    
                ret[g_count++] = root->val;
            }
            ADD(root->left, depth-tmpr-2, ret);
            return tmpr+1;
        }
        return -1;
    }   

}

int* distanceK(struct TreeNode* root, struct TreeNode* target, int K, int* returnSize) {
    
    
    int nums = GetNodeNum(root);
    int *ret = malloc(sizeof(int) * nums);
    g_count = 0;
    DFS(root, target, K, ret);    
    *returnSize = g_count;
    return ret;
}

leetcode101题,验证对称二叉树。我用队列解的,结果题解惊为天人,脑子不好使啊->直接搞两个入参的递归,没见过

bool getresult(struct TreeNode* l, struct TreeNode* r)
{
    
    
    if(l==NULL && r==NULL) {
    
    
        return true;
    }
    if (l==NULL || r==NULL) {
    
    
        return false;
    }
    if(l->val != r->val) {
    
    
        return false;
    }
    return getresult(l->left, r->right) && getresult(l->right, r->left);
}


bool isSymmetric(struct TreeNode* root){
    
    
    if(root == NULL) {
    
    
        return true;
    }
    return getresult(root, root);
}

leetcode776题,拆分二叉搜索树。题目要求给一个二叉搜索树,及目标值V,<=V的放到一个集合里面,>V的放到一个集合里面,毫无思路,感谢大牛共享的答案吧。理解一遍。
递归理解:ret[0]放小于等于V的树的根节点,ret[1]放大于V的树的根节点。
1、如果当前节点为空,就返回空。
2、如果当前节点小于目标值,那么说明当前节点的左子树也满足要求的,那么把当前ret[0]存给节点。并往右找
3、如果当前节点大于目标值,那么说明当前节点的左子树中是有问题的,那么把当前ret[1]存给节点。
好难,看不懂,要放弃了= =
在这里插入图片描述

struct TreeNode** ret;
void dfs(struct TreeNode* root, int V) {
    
    
	if (root == NULL) {
    
    
		return;
	}
	if (root->val <= V) {
    
    
		dfs(root->right, V);
		root->right = ret[0];
		ret[0] = root;
	}
	else {
    
    
		dfs(root->left, V);
		root->left = ret[1];
		ret[1] = root;
	}
	return;
}
struct TreeNode** splitBST(struct TreeNode* root, int V, int* returnSize) {
    
    
	ret = (struct TreeNode**)malloc(sizeof(struct TreeNode *) * 2);
	ret[0] = NULL;
	ret[1] = NULL;
	*returnSize = 2;
	dfs(root, V);
	return ret;
}

leetcode98题,修建二叉搜索树

struct TreeNode* trimBST(struct TreeNode* root, int L, int R){
    
    
    if(root == NULL) {
    
    
        return NULL;
    }
    root->right = trimBST(root->right, L, R);
    root->left = trimBST(root->left, L, R);
    if(root->val < L) {
    
    
        return root->right;
    }
    if(root->val > R) {
    
    
        return root->left;
    }
    return root;    
}

leetcode98题,验证二叉搜索树。
思路是递归的,但是从树的角度介绍吧
简单来说就是把所有异常场景找出来
1、如果当前节点为空,那么就是ok的
2、如果当前节点左子树不为空,那么要看左子树最大的数字跟当前节点比较;
3、如果当前节点右子树不为空,那么要看右子树最小的数字跟当前节点比较;
4、依次确认该节点的左右节点是否满足要求。

bool isValidBST(struct TreeNode* root){
    
    
    if (root == NULL) {
    
    
        return 1;
    }
    struct TreeNode* tmp;
    tmp = root ->left;
    if (tmp != NULL){
    
    
        while (tmp -> right != NULL){
    
    
            tmp = tmp->right;
        }
        if (tmp->val >= root->val)
            return 0;
    }
    tmp = root ->right;
    if (tmp != NULL){
    
    
        while(tmp->left != NULL){
    
    
            tmp = tmp->left;
        }
        if (tmp->val <= root->val)
            return 0;
    }
    
    bool a = isValidBST(root->left);
    bool b = isValidBST(root->right);
    if (a == 0 || b == 0){
    
    
        return 0;
    }
    return 1;

}

猜你喜欢

转载自blog.csdn.net/weixin_45554139/article/details/104640448