
- 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 all odd nodes of Binary Search Tree in C++
In this problem, we are given a binary search tree and we have to print all the nodes that have odd values.
The binary search tree is a special type of tree that possess the following properties −
The left subtree always has values smaller than the root node.
The right subtree always has values larger than the root node.
Both the left and right subtree should also follow the above two properties.
Let’s take an example to understand the problem −
Output − 1 3 9
To solve this problem, a simple approach would be traversing the tree. On traversal, we will check the value of each node of the tree. If the node is odd print it otherwise more to the next node of the tree.
The complexity of the program will depend on the number of nodes. Time complexity: O(n).
Example
The below program shows the implementation of our solution −
#include <bits/stdc++.h> using namespace std; struct Node { int key; struct Node *left, *right; }; Node* newNode(int item){ Node* temp = new Node; temp->key = item; temp->left = temp->right = NULL; return temp; } Node* insertNode(Node* node, int key){ if (node == NULL) return newNode(key); if (key < node->key) node->left = insertNode(node->left, key); else node->right = insertNode(node->right, key); return node; } void printOddNodes(Node* root){ if (root != NULL) { printOddNodes(root->left); if (root->key % 2 != 0) cout<<root->key<<"\t"; printOddNodes(root->right); } } int main(){ Node* root = NULL; root = insertNode(root, 6); root = insertNode(root, 3); root = insertNode(root, 1); root = insertNode(root, 4); root = insertNode(root, 9); root = insertNode(root, 8); root = insertNode(root, 10); cout<<"Nodes with odd values are :\n"; printOddNodes(root); return 0; }
Output
Nodes with odd values are −
1 3 9
- Related Articles
- Print all even nodes of Binary Search Tree in C++
- Print all internal nodes of a Binary tree in C++
- Print all full nodes in a Binary Tree in C++
- Print Levels of all nodes in a Binary Tree in C++ Programming.
- Print all nodes in a binary tree having K leaves in C++
- Print all nodes between two given levels in Binary Tree in C++
- Print all leaf nodes of a binary tree from right to left in C++
- Print the nodes at odd levels of a tree in C++ Programming.
- Print Common Nodes in Two Binary Search Trees in C++
- Product of all leaf nodes of binary tree in C++
- Product of all nodes in a Binary Tree in C++
- All Nodes Distance K in Binary Tree in C++
- Print All Leaf Nodes of a Binary Tree from left to right using Iterative Approach in C++
- Print leftmost and rightmost nodes of a Binary Tree in C Program.
- Program to print path from root to all nodes in a Complete Binary Tree using C++
