-
-
Save dlgusdn616/43be903ca33c9a84bbee874a197b2acb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef struct TreeNode { | |
int data; | |
struct TreeNode *left, *right; | |
} TreeNode; | |
int get_height(TreeNode *root) | |
// 아래와 같은 트리 구조를 생성한다. | |
// n1 | |
// n3 n2 | |
// n4 n5 | |
void main() { | |
TreeNode n4 = { 500, NULL, NULL }; | |
TreeNode n5 = { 200, NULL, NULL }; | |
TreeNode n3 = { 100, &n4, &n5 }; | |
TreeNode n2 = { 50, NULL, NULL }; | |
TreeNode n1 = { 0, &n2, &n3 }; | |
printf("디렉토리의 크기 = %d\n", calc_direc_size(&n1)); | |
} | |
int get_height(TreeNode *root) { // 트리의 높이를 구한다. | |
if (!root) | |
return 0; | |
else { | |
int left_h = get_height(root->left); // 왼쪽 서브트리의 높이를 순환호출을 통해 얻는다. | |
int right_h = get_height(root->right); // 같은 방법으로 오른쪽 서브트리의 높이를 얻는다. | |
return 1 + (left_h > right_h ? left_h : right_h); // 둘 중 큰 값에 1을 더해 반환한다. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment