• 2022_08_10_106期__二叉树


    目录

     

    求第k层的节点个数:

    返回x所在的节点。

    力扣:


    求第k层的节点个数:

    我们要求第k层的节点的个数:

    假如我们的k为1时,对应的节点个数为1,加入我们的k为2时,我们可以求1节点对应的子节点数目,加入我们的k为3时,我们可以求第2层的节点对应的子节点的数目:

     我们画图演示求第三层的节点个数

     2的左子节点为3,3的左子节点为NULL,3的右节点为NULL,2的右节点为NULL。所以只有一个子节点,返回1

    4的左节点为5,5的左右节点都为NULL,4的右节点为6,6的左右节点都为NULL,所以4节点的子节点有两个,分别是5和6.

    所以第三层的节点的个数为1+2等于3.

    我们用代码进行实现:

    1. int TreeKLevel(BTNode*root, int k)
    2. {
    3. assert(k > 0);
    4. if (k == 1)
    5. {
    6. return 1;
    7. }
    8. if (root== NULL)
    9. {
    10. return 0;
    11. }
    12. return TreeKLevel(root->left,k-1)
    13. + TreeKLevel(root->right,k-1);
    14. }

    所以代码的核心思想就是求第k-1层节点对应的子节点的数目。

    返回x所在的节点。

    1. BTNode*TreeFind(BTNode*root, BTDataType x)
    2. {
    3. if (root == NULL)
    4. return NULL;
    5. if (root->data == x)
    6. return root;
    7. BTNode*lret = TreeFind(root->left, x);
    8. if (lret)
    9. return lret->data;
    10. BTNode*rret = TreeFind(root->right, x);
    11. if (rret)
    12. return rret->data;
    13. return NULL;
    14. }

    力扣:

    力扣

    1. class Solution {
    2. public:
    3. bool isSameTree(TreeNode* p, TreeNode* q) {
    4. if(p==NULL&&q==NULL)
    5. {
    6. return true;
    7. }
    8. if(p==NULL||q==NULL)
    9. {
    10. return false;
    11. }
    12. if(p->val!=q->val)
    13. {
    14. return false;
    15. }
    16. return isSameTree(p->left,q->left)
    17. &&isSameTree(p->right,q->right);
    18. }
    19. };

    当p和q为空指针时,表示是两个空树,空树也是相同的数。

    当p和q有一个为空指针时,p和q对应的值一定不相同,对应的树一定不相等。

    当p和q都不为空指针,并且p对应的值和q对应的值不相等的时候,对应的树不相等。

    剩下的结果就是p和q对应的值相等。

    1. return isSameTree(p->left,q->left)
    2. &&isSameTree(p->right,q->right);

    当两个树对应的左子节点和右子节点都相等的时候,返回的为真。

    力扣

    1. class Solution {
    2. public:
    3. bool isUnivalTree(TreeNode* root) {
    4. if(root==NULL)
    5. {
    6. return true;
    7. }
    8. if(root->left&&root->val!=root->left->val)
    9. {
    10. return false;
    11. }
    12. if(root->right&&root->val!=root->right->val)
    13. {
    14. return false;
    15. }
    16. return isUnivalTree(root->left)
    17. &&isUnivalTree(root->right);
    18. }
    19. };

    力扣

    1. bool isSameTree(TreeNode* p, TreeNode* q) {
    2. if(p==NULL&&q==NULL)
    3. {
    4. return true;
    5. }
    6. if(p==NULL||q==NULL)
    7. {
    8. return false;
    9. }
    10. if(p->val!=q->val)
    11. {
    12. return false;
    13. }
    14. return isSameTree(p->left,q->left)
    15. &&isSameTree(p->right,q->right);
    16. }
    17. class Solution {
    18. public:
    19. bool isSubtree(TreeNode* root, TreeNode* subRoot) {
    20. if(root==NULL)
    21. return false;
    22. if(isSameTree(root,subRoot))
    23. return true;
    24. return isSubtree(root->left,subRoot)
    25. ||isSubtree(root->right,subRoot);
    26. }
    27. };

    力扣

    1. int TreeSize(struct TreeNode*root)
    2. {
    3. if(root==NULL)
    4. return 0;
    5. return TreeSize(root->left)+TreeSize(root->right)+1;
    6. }
    7. void preorder(int *a,struct TreeNode*root,int i)
    8. {
    9. if(root==NULL)
    10. return ;
    11. a[i++]=root->val;
    12. preorder(a,root->left,i);
    13. preorder(a,root->right,i);
    14. }
    15. int* preorderTraversal(struct TreeNode* root, int* returnSize){
    16. int n=TreeSize(root);
    17. int*a=(int*)malloc(sizeof(int)*n);
    18. *returnSize=n;
    19. int i=0;
    20. preorder(a,root,i);
    21. return a;
    22. }

    我们进行测试:

     问题出现在i这里

    1. void preorder(int *a,struct TreeNode*root,int i)
    2. {
    3. if(root==NULL)
    4. return ;
    5. a[i++]=root->val;
    6. preorder(a,root->left,i);
    7. preorder(a,root->right,i);
    8. }

    我们的i的变化并不会影响接下来递归中的i值,我们画图进行解释:

    假如我们要前序遍历的数组元素为1,2,3

     我们先放1,i++

    我们再放2,i++。

    但是我们这里的i++并不会影响preorder(a,root->right,i)中的i,所以我们把3也放在了2处,所以对应的结果为:

    正确的写法:

    1. int TreeSize(struct TreeNode*root)
    2. {
    3. if(root==NULL)
    4. return 0;
    5. return TreeSize(root->left)+TreeSize(root->right)+1;
    6. }
    7. void preorder(int *a,struct TreeNode*root,int*pi)
    8. {
    9. if(root==NULL)
    10. return ;
    11. a[*pi]=root->val;
    12. (*pi)++;
    13. preorder(a,root->left,pi);
    14. preorder(a,root->right,pi);
    15. }
    16. int* preorderTraversal(struct TreeNode* root, int* returnSize){
    17. int n=TreeSize(root);
    18. int*a=(int*)malloc(sizeof(int)*n);
    19. *returnSize=n;
    20. int i=0;
    21. preorder(a,root,&i);
    22. return a;
    23. }

  • 相关阅读:
    Python多方法高效匹配、关联操作两个列表
    2.1 - 操作系统的作用、分类
    c 取字符串中的子串
    CodeForces Round #832 (div.2) A~C
    Spring/IoC、DI、Bean
    MATLAB库函数resample(重采样函数)的C语言实现【姊妹篇2纯C语言实现】
    2022建筑架子工(建筑特殊工种)考试练习题及在线模拟考试
    Jenkins 安装 NodeJS 插件后无法识别Node环境:env node No such file or directory
    PHP下载文件
    图片批处理工具 PhotoMill X直装 for mac
  • 原文地址:https://blog.csdn.net/qq_66581313/article/details/126516963