C출력 결과보통 · 20점#구조체#포인터#재귀#트리

165. 구조체 트리 후위 순회의 k번째 노드

다음 C 프로그램의 실행 결과를 쓰시오.

1#include <stdio.h>
2
3struct Node {
4 int val;
5 struct Node *left;
6 struct Node *right;
7};
8
9int cnt = 0;
10int found = -1;
11
12void post(struct Node *n, int k) {
13 if (n == NULL) return;
14 post(n->left, k);
15 post(n->right, k);
16 cnt++;
17 if (cnt == k) found = n->val;
18}
19
20int main() {
21 struct Node d = {4, NULL, NULL};
22 struct Node e = {9, NULL, NULL};
23 struct Node b = {2, &d, NULL};
24 struct Node c = {7, NULL, &e};
25 struct Node a = {5, &b, &c};
26 post(&a, 3);
27 printf("%d %d\n", found, cnt);
28 return 0;
29}

해설을 먼저 보면 이후 정답 시 점수가 50%만 반영됩니다.