-
-
Save dlgusdn616/32698a9a31293263199fea697f0a5f60 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_leaf(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", get_leaf(&n1)); | |
} | |
int get_leaf(TreeNode *root) { // 트리에 존재하는 단말노드의 갯수를 구한다. | |
if (!root) | |
return 0; | |
if (root->left == NULL && root->right == NULL) | |
return 1; | |
else | |
return get_leaf(root->left) + get_leaf(root->right); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment