【发布时间】:2016-11-24 05:36:40
【问题描述】:
我创建了一个二叉树,它具有三个值,即整数邮政编码、状态字符串和城市字符指针。我正在尝试计算同一州有多少个城市(邮政编码)。因此,我在下面写了一个函数,但它不起作用。(格式与此处发布的输入文件相同)希望有人能帮助我。enter image description here
typedef struct n_ {
int zipCode; // A zip code that exists in the given city/state
char *city; // Will point to a city name
char state[3]; // A state abbreviation. Note that we need
// room for the NULL terminator!
struct n_ *left; //connections to other nodes
struct n_ *right;
} Node;
int findStateCount(Node *root, char *state) {
int count = 0;
if (root!=NULL) {
findStateCount(root->left, state);
if (strcmp((root-> state), (state)) == 0)
++count;
findStateCount(root->right, state);
}
return count;
}
【问题讨论】:
-
它不起作用 - 怎么办?你对递归调用的结果做了什么?看起来什么都没有,因为
count对于每个调用实例都是本地的。
标签: c recursion binary-tree