#include<stdio.h>
#include<stdlib.h>

typedef struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
} TreeNode;

int count(TreeNode* node) {
    if (node == NULL) return 0;
    if (node->left == NULL && node->right != NULL) {
        return 1 + count(node->right);
    }
    if (node->left != NULL && node->right == NULL) {
        return 1 + count(node->left);
    }
    return count(node->left) + count(node->right);
}

int main() {
    TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
    root->val = 1;
    TreeNode* left = (TreeNode*)malloc(sizeof(TreeNode));
    left->val = 2;
    TreeNode* right = (TreeNode*)malloc(sizeof(TreeNode));
    right->val = 3;
    root->left = left;
    root->right = right;
    printf("The number of single branch nodes in the binary tree is: %d\n", count(root));
    return 0;
}
``
用c语言编写求二叉树单分支结点个数递归运算

原文地址: https://www.cveoy.top/t/topic/g9fV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录