Print all odd nodes of Binary Search Tree in C++



A Binary Search Tree is a data structure that stores data in a sorted order such that for every node, the left subtree contains values less than the node's value, and the right subtree contains values greater than the node's value. In this article, we will solve the problem of finding all odd nodes of a binary search tree in C++.

Find Odd-Valued Nodes in BST

You are given a binary search tree (BST) as input and your task is to write a program that finds all the nodes with odd values and returns them as output. To understand better, let's consider the following example:

Scenario 1

Input:
        8
       / \
      3   10
     / \    \
    1   6    14
       / \   /
      4   7 13
Output:
1 3 7 13

Explanation: The odd-valued nodes in the BST are 1, 3, 7, and 13.

Scenario 2

Input:
        10
       / \
      2   15
Output:
[ ]

Explanation: Return an empty array since there are no odd-valued nodes in the BST.

Steps to Find Odd Nodes in BST

To find all odd node in a BST, we need to traverse the tree and check if the value of each node is odd. There are several tree traversal algorithms, such as in-order, pre-order, and post-order. For this problem, we will use the in-order traversal method, because it visits nodes in ascending order, so our result will also be sorted.

  • Perform an in-order traversal of the BST.
  • During the traversal, check if the current node's value is odd.
  • If it is odd, add it to the result list.
  • Continue the traversal until all nodes have been visited.
  • Return the list of odd-valued nodes.

C++ Code to Find Odd Nodes in BST

Here is a C++ implementation to find all odd nodes in a BST:

#include <iostream>
#include <vector>
using namespace std;

// Definition of the Node structure for the binary search tree
struct Node {
    int data;
    Node* left;
    Node* right;
    
    Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

// Function to perform in-order traversal and collect odd nodes
void collectOddNodes(Node* root, vector<int>& oddNodes) {
    if (root == nullptr) return;

    collectOddNodes(root->left, oddNodes);
    if (root->data % 2 != 0) {
        oddNodes.push_back(root->data);
    }
    collectOddNodes(root->right, oddNodes);
}

int main(){
    // Create BST ( as there in Scenario 1 )
    Node* root = new Node(8);
    root->left = new Node(3);
    root->right = new Node(10);
    root->left->left = new Node(1);
    root->left->right = new Node(6);
    root->left->right->left = new Node(4);
    root->left->right->right = new Node(7);
    root->right->right = new Node(14);
    root->right->right->left = new Node(13);

    vector<int> oddNodes;
    collectOddNodes(root, oddNodes);

    cout << "Odd nodes in the BST: ";
    for (int val : oddNodes) {
        cout << val << " ";
    }
    cout << endl;

    return 0;
}
Odd nodes in the BST: 1 3 7 13

Time and Space Complexity

The time complexity of inorder traversal in a BST is O(N), where N is the number of nodes in the tree.

The space complexity of this implementation is O(N) to store the odd-valued nodes in the result vector.

Farhan Muhamed
Farhan Muhamed

No Code Developer, Vibe Coder

Updated on: 2025-08-18T11:30:02+05:30

294 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements