【算法笔记】求双分支节点个数

int countDsonNodes(bitree b)
{ int a=0;
    if(b==NULL)
        return 0;
    if(b->lchild!=NULL&&b->rchild!=NULL)
        a++;

    a=a+countDsonNodes(b->lchild);

    a=a+countDsonNodes(b->rchild);
    return a;
}

完整代码

#include <iostream>

using namespace std;

struct TreeNode {
    int data;
    struct TreeNode* lchild;
    struct TreeNode* rchild;
};

typedef struct TreeNode* bitree;

bitree createNode(int data) {
    bitree newNode = new TreeNode;
    newNode->data = data;
    newNode->lchild = NULL;
    newNode->rchild = NULL;
    return newNode;
}

int countDsonNodes(bitree b)
{ int a=0;
    if(b==NULL)
        return 0;
    if(b->lchild!=NULL&&b->rchild!=NULL)
        a++;

    a=a+countDsonNodes(b->lchild);

    a=a+countDsonNodes(b->rchild);
    return a;
}

int main() {
    // 创建二叉树
    bitree root = createNode(1);
    root->lchild = createNode(2);
    root->rchild = createNode(3);
    root->lchild->lchild = createNode(4);
    root->lchild->rchild = createNode(5);
    root->rchild->lchild = createNode(6);
    root->rchild->rchild = createNode(7);

    // 统计度为2的节点个数
    int dsonCount = countDsonNodes(root);
    cout << "度为2的节点个数为:" << dsonCount << endl;

    return 0;
}

通过本题注意用递归时的返回值

错误代码:

int dsonnodes(bitree b)
{ int a=0;
    if(b==NULL)
        return 0;
    if(b.lchild!=NULL&&b.rchild!=NULL)
        a++;
   
    dsonnodes(b.lchild);
   
    dsonnodes(b.rchild);
    return a;
}

没有将左右结果返回

完整代码

#include <iostream>

// 二叉树节点结构体
struct TreeNode {
    int data;
    TreeNode* lchild;
    TreeNode* rchild;
    TreeNode(int val) : data(val), lchild(nullptr), rchild(nullptr) {}
};

// 判断二叉树节点是否有两个子节点
int countNodesWithTwoChildren(TreeNode* root) {
    if (root == nullptr) {
        return 0;
    }

    int count = 0;

    if (root->lchild != nullptr && root->rchild != nullptr) {
        count = 1;
    }

    // 递归遍历左子树和右子树
    int leftCount = countNodesWithTwoChildren(root->lchild);
    int rightCount = countNodesWithTwoChildren(root->rchild);

    // 返回当前子树和左右子树中满足条件的节点数量之和
    return count + leftCount + rightCount;
}

int main() {
    // 创建示例二叉树
    TreeNode* root = new TreeNode(1);
    root->lchild = new TreeNode(2);
    root->rchild = new TreeNode(3);
    root->lchild->lchild = new TreeNode(4);
    root->lchild->rchild = new TreeNode(5);
    root->rchild->lchild = new TreeNode(6);
    root->rchild->rchild = new TreeNode(7);

    // 计算具有两个子节点的节点数量
    int result = countNodesWithTwoChildren(root);

    std::cout << "Number of nodes with two children: " << result << std::endl;

    // 释放动态分配的内存,防止内存泄漏
    delete root->rchild->rchild;
    delete root->rchild->lchild;
    delete root->rchild;
    delete root->lchild->rchild;
    delete root->lchild->lchild;
    delete root->lchild;
    delete root;

    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值