Print all even 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 find all even nodes of a binary search tree using C++.

Find Even-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 even 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: 4 6 8 10 14
Explanation: The even-valued nodes in the BST are 4, 6, 8, 10, and 14.

Scenario 2

Input:
        9
       / \
      1   11
Output: []
Explanation: Return an empty array since there are no even-valued nodes in the BST.

Steps to Find Even Nodes in a BST

To find all even nodes in a BST, we need to traverse the tree and check if the value of each node is even. 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 even.
  • If it is even, add it to the result list.
  • Continue the traversal until all nodes have been visited.
  • Return the list of even-valued nodes.

C++ Code to Find Even Nodes in BST

Following is a C++ implementation to find all even 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 even nodes
void collectEvenNodes(Node* root, vector<int>& evenNodes) {
   if (root == nullptr) return;
   
   collectEvenNodes(root->left, evenNodes);
   if (root->data % 2 == 0) {
      evenNodes.push_back(root->data);
   }
   collectEvenNodes(root->right, evenNodes);
}

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> evenNodes;
   collectEvenNodes(root, evenNodes);
   
   cout << "Even nodes in the BST: ";
   for (int val : evenNodes) {
      cout << val << " ";
   }
   cout << endl;
   
   return 0;
}

Following is the output of the above program:

Even nodes in the BST: 4 6 8 10 14 

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 even-valued nodes in the result vector.

Farhan Muhamed
Farhan Muhamed

No Code Developer, Vibe Coder

Updated on: 2025-08-12T17:00:45+05:30

385 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements