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

typedef struct node{
    int value;
    struct node *leftChild;
    struct node *rightChild;
}Tree;


int countNode(Tree *root)
{
    if(!root) return 0;
    else{
        int left = countNode(root->leftChild);
        int right = countNode(root->rightChild);
        return left + right + 1;
    }
}

// just create a tree without any skill
void createTree(Tree *root)
{
    /*   1
       2    3
     4   5   6 */

    Tree *node[5];
    for(int i = 0; i<5; i++){
        node[i] = (Tree *)malloc(sizeof(Tree *));
        node[i]->value = i+2;
    }

    root->value = 1;
    root->leftChild = node[0];
    root->rightChild = node[1];
    node[0]->leftChild = node[2];
    node[0]->rightChild = node[3];
    node[1]->rightChild = node[4];
}


int main()
{
    Tree *Tree_A = (Tree *)malloc(sizeof(Tree));
    createTree(Tree_A);
    printf("node# = %d\n", countNode(Tree_A));  
    return 0;
}

arrow
arrow
    全站熱搜

    大神(偽) 發表在 痞客邦 留言(0) 人氣()