TA的每日心情 | 衰 2023-2-12 01:11 |
---|
签到天数: 3 天 [LV.2]偶尔看看I
VIP
- 积分
- 2534
|
发表于 2022-12-12 16:51:43
|
显示全部楼层
#include <stdio.h>
#include <string.h>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
typedef struct TreeNode Node;
void visit(Node *n) {
/********* Begin *********/
printf("%d\n",n->val);
/********* End *********/
}
void inorder(Node *root, void (*visit)(Node *)) {
if (root == NULL)
return;
else {
inorder(root->left, visit);
(*visit)(root);
inorder(root->right, visit);
}
}
int main(void) {
Node nodes[128];
memset(nodes, 0, sizeof(nodes));
Node *root = &nodes[0];
int num, capacity;
scanf("%d",&num);
capacity = num;
while(num--){
scanf("%d",&nodes[capacity-num-1].val);
}
/********* Begin *********/
for(int i = 1; i < capacity; i++){
if((i-1)&1) nodes[(i-1)/2].right = &nodes[i];
else nodes[(i-1)/2].left = &nodes[i];
}
/********* End *********/
inorder(root, &visit);
return 0;
} |
|