gpt4 book ai didi

c - 二叉树函数

转载 作者:太空宇宙 更新时间:2023-11-03 23:35:38 25 4
gpt4 key购买 nike

有人可以解释在下面的二叉树上调用 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)

关于c - 二叉树函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4467252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com