이진트리 삽입 & Node 구하는 코드
void insert(int root, int key) {
if (root < key && rightC[root] == 0) { //no child
rightC[root] =key;
return;
}
else if (root > key && leftC[root] == 0) { //no child
leftC[root] = key;
return;
}
//right fill s
if (root < key && rightC[root] != 0) {
insert(rightC[root], key);
}
//left child fill
else if (root > key && leftC[root] != 0) { //왼쪽 자식이 비어있을때
insert(leftC[root], key);
}
}
//해당하는 노드의 자식수를 구하는 함수
int child_cnt(int curNode) {
//no child -- > 0s
if (leftC[curNode] == 0 && rightC[curNode] == 0) {
return 0;
}
//both left/left +2
if (leftC[curNode] != 0 && rightC[curNode] != 0)
return child[curNode] = child_cnt(leftC[curNode]) + child_cnt(rightC[curNode]) + 2;
//only lelf child +1
else if (leftC[curNode] != 0 && rightC[curNode] == 0) {
return child[curNode] = child_cnt(leftC[curNode]) + 1;
}
//right child num + 1
else if (leftC[curNode] == 0 && rightC[curNode] != 0) {
return child[curNode] = child_cnt(rightC[curNode]) + 1;
}
}