다음 tree_height() 함수는 이진 트리의 루트 노드를 매개변수로 받아 트리의 높이를 반환한다. (가) ~ (다)에 들어갈 내용을 바르게 연결한 것은?
typedef struct node {
int value;
struct node *left;
struct node *right;
} node;
int tree_height (node *ptr) {
int left_h, right_h;
if (ptr == NULL)
return 0;
else {
left_h = tree_height(ptr->left);
right_h = tree_height(ptr->right);
if ( (가) )
return (나) ;
else
return (다) ;
}
} (가) (나) (다)
- ①left_h > right_h left_h right_h
- ②left_h > right_h left_h + 1 right_h + 1
- ③left_h < right_h left_h right_h
- ④left_h < right_h left_h + 1 right_h + 1
선지를 골라 바로 채점해 보세요.
정답 ②
출처: 인사혁신처 공개 기출(공공데이터포털, 이용허락 제한 없음)