バイナリツリーのノードのルートからの距離を求めなければなりません。 私の解決策は次のとおりです。ルートからバイナリツリーのノードまでの距離
int distanceUtil(struct node* root, int x, int dist) {
if (root == NULL) {
return -1;
}
else {
if (root->data == x) {
return dist;
}
return distanceUtil(root->left, x, dist + 1) || distanceUtil(root->right, x, dist + 1);
}
}
int distance(struct node* root, int x) {
return distanceUtil(root, x, 0);
}
が、メインに私が行うとき、それはwork.In事実をしていません。
struct node* root = newNode(12);
root->left = newNode(8);
root->right = newNode(18);
root->left->left = newNode(2);
root->left->right = newNode(9);
root->right->left = newNode(15);
root->right->right = newNode(22);
root->right->right->right = newNode(33);
printf("il cammino : %d", distance(root,33));
getchar();
return 0;
それは1を返しますが、誰かが私を助けることができるそれが3を返す必要がありますか? ありがとうございます。
'' ||のみのいずれかを返す0または1 https://stackoverflow.com/a/18098119/3072566 – litelite