
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print the nodes at odd levels of a tree in C++ Programming.
Given the binary tree, the program must print the nodes at odd levels of a tree and the levels of a binary tree start from 1 to n.
As nothing is mentioned one of the two approaches can be implemented i.e. recursion or iteration.
Since we are using a recursive approach, the program will make a recursive call to a function that will be fetching the nodes at odd levels and returning them.
In the above binary tree −
Nodes at level 1: 10 Nodes at level 2: 3 and 211 Nodes at level 3: 140, 162, 100 and 146
So, the nodes at level 1 and level 3 will be printed that means the output will be 10, 140, 162, 100 and 146.
Algorithm
START Step 1 -> create a structure of a node as struct Node struct node *left, *right int data End Step 2 -> function to create a node node* newnode(int data) node->data = data node->left = node->right = NULL; return (node) step 3 -> create function for finding the odd nodes void odd(Node *root, bool ifodd = true) IF root = NULL Return End if (ifodd) print root->data End odd(root->left, !ifodd) odd(root->right, !ifodd) step 4 -> In main() Create tree using Node* root = newnode(45) root->left = newnode(23) Call odd(root) Stop
Example
#include <bits/stdc++.h> using namespace std; struct Node{ int data; Node* left, *right; }; void odd(Node *root, bool ifodd = true){ if (root == NULL) return; if (ifodd) cout << root->data << " " ; odd(root->left, !ifodd); odd(root->right, !ifodd); } // function to create a new node Node* newnode(int data){ Node* node = new Node; node->data = data; node->left = node->right = NULL; return (node); } int main(){ Node* root = newnode(45); root->left = newnode(23); root->right = newnode(13); root->left->left = newnode(24); root->left->right = newnode(85); cout<<"\nodd nodes are "; odd(root); return 0; }
Output
if we run the above program then it will generate the following output
odd nodes are 45 24 85
- Related Articles
- Program to print the nodes at odd levels of a tree using C++
- Print Levels of all nodes in a Binary Tree in C++ Programming.
- C++ Program to Print only Odd Numbered Levels of a Tree
- Print all odd nodes of Binary Search Tree in C++
- Print all the levels with odd and even number of nodes in it in C++
- Print all nodes between two given levels in Binary Tree in C++
- Print path between any two nodes in a Binary Tree in C++ Programming.
- Print the nodes of binary tree as they become the leaf node in C++ Programming.
- Print all internal nodes of a Binary tree in C++
- Print Binary Tree levels in sorted order in C++
- Print all full nodes in a Binary Tree in C++
- Print all even nodes of Binary Search Tree in C++
- Print leftmost and rightmost nodes of a Binary Tree in C Program.
- Maximum sum of non-leaf nodes among all levels of the given binary tree in C++
- Program to print nodes in the Top View of Binary Tree using C++

Advertisements