有人能解释一下在下面的二叉树上调用mystery(root)
的输出吗?
struct treenode {
int data;
struct treenode* left;
struct treenode* right;
}
void mystery(struct treenode* root) {
if (root == NULL) return;
mystery(root->right);
printf("%d ", root->data%25);
mystery(root->left);
}
树
35
/ \
23 17
/ \ /
89 135 56
/ \
44 89
\
74
/
287
最佳答案
请参阅以下跟踪:
You call mystery(35)
the root is not null which calls mystery(17)
mystery(17) is not null and calls mystery(17-Right)
This is null so it Returns
printf(17%25 == 17)
call mystery(56)
mystery(56) is not null and calls mystery(89)
mystery(89) is not null and calls mystery(74)
mystery(74) is not null and calls mystery(74-Right)
mystery(74-Right) is null; return;
printf(74%25 == 24)
mystery(74) calls mystery(287)
printf(287%25 == 12)
printf(89%25 == 14)
printf(56%25 == 6)
mystery(56) calls mystery(44)
mystery(44) has no right or left child
printf(44%25 == 19)
printf(35%25 == 10)
root calls mystery(23)
mystery(23) calls mystery(135)
printf(135%25 == 10)
printf(23%25 == 23)
mystery(23) calls mystery(89)
printf(89%25 == 14)