이 튜토리얼에서는 주어진 바이너리 트리의 홀수 레벨에 있는 노드를 출력하는 프로그램에 대해 논의할 것입니다.
이 프로그램에서 루트 노드의 레벨은 1로 간주되고 동시에 대체 레벨은 다음 홀수 레벨입니다.
예를 들어 다음 이진 트리가 주어진다고 가정해 보겠습니다.

그러면 이 이진 트리의 홀수 레벨에 있는 노드는 1, 4, 5, 6이 됩니다.
예시
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* left, *right;
};
//printing the nodes at odd levels
void print_onodes(Node *root, bool is_odd = true){
if (root == NULL)
return;
if (is_odd)
cout << root->data << " " ;
print_onodes(root->left, !is_odd);
print_onodes(root->right, !is_odd);
}
//creating a new node
struct Node* create_node(int data){
struct Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}
int main(){
struct Node* root = create_node(13);
root->left = create_node(21);
root->right = create_node(43);
root->left->left = create_node(64);
root->left->right = create_node(85);
print_onodes(root);
return 0;
} 출력
13 64 85