Given a binary search tree. The task is to print all even nodes of the binary search tree.
Examples:
Input : 
          5 
        /   \ 
       3     7 
      / \   / \ 
     2   4 6   8 
Output : 2 4 6 8
Input :
          14 
        /   \ 
       12    17 
      / \   / \ 
     8  13 16   19 
Output : 8 12 14 16
Approach: Traverse the Binary Search tree and check if current node’s value is even. If yes then print it otherwise skip that node.
Below is the implementation of the above Approach:
C++
| // C++ program to print all even node of BST#include <bits/stdc++.h>usingnamespacestd;// create TreestructNode {    intkey;    structNode *left, *right;};// A utility function to create a new BST nodeNode* newNode(intitem){    Node* temp = newNode;    temp->key = item;    temp->left = temp->right = NULL;    returntemp;}// A utility function to do inorder traversal of BSTvoidinorder(Node* root){    if(root != NULL) {        inorder(root->left);        cout << root->key << " ";        inorder(root->right);    }}/* A utility function to insert a new node   with given key in BST */Node* insert(Node* node, intkey){    /* If the tree is empty, return a new node */    if(node == NULL)        returnnewNode(key);    /* Otherwise, recur down the tree */    if(key < node->key)        node->left = insert(node->left, key);    else        node->right = insert(node->right, key);    /* return the (unchanged) node pointer */    returnnode;}// Function to print all even nodesvoidevenNode(Node* root){    if(root != NULL) {        evenNode(root->left);        // if node is even then print it        if(root->key % 2 == 0)            cout << root->key << " ";        evenNode(root->right);    }}// Driver Codeintmain(){    /* Let us create following BST       5      / \     3     7    / \ / \    2 4 6 8 */    Node* root = NULL;    root = insert(root, 5);    root = insert(root, 3);    root = insert(root, 2);    root = insert(root, 4);    root = insert(root, 7);    root = insert(root, 6);    root = insert(root, 8);    evenNode(root);    return0;} | 
Java
| // Java program to print all even node of BST classGfG { // create Tree staticclassNode {     intkey;     Node left, right; }// A utility function to create a new BST node staticNode newNode(intitem) {     Node temp = newNode();     temp.key = item;     temp.left = null;    temp.right = null;     returntemp; } // A utility function to do inorder traversal of BST staticvoidinorder(Node root) {     if(root != null) {         inorder(root.left);         System.out.print(root.key + " ");         inorder(root.right);     } } /* A utility function to insert a new node with given key in BST */staticNode insert(Node node, intkey) {     /* If the tree is empty, return a new node */    if(node == null)         returnnewNode(key);     /* Otherwise, recur down the tree */    if(key < node.key)         node.left = insert(node.left, key);     else        node.right = insert(node.right, key);     /* return the (unchanged) node pointer */    returnnode; } // Function to print all even nodes staticvoidevenNode(Node root) {     if(root != null) {         evenNode(root.left);         // if node is even then print it         if(root.key % 2== 0)             System.out.print(root.key + " ");         evenNode(root.right);     } } // Driver Code publicstaticvoidmain(String[] args) {     /* Let us create following BST     5     / \     3     7     / \ / \     2 4 6 8 */    Node root = null;     root = insert(root, 5);     root = insert(root, 3);     root = insert(root, 2);     root = insert(root, 4);     root = insert(root, 7);     root = insert(root, 6);     root = insert(root, 8);     evenNode(root); }}  | 
Python3
| # Python3 program to print all even node of BST # create Tree # to create a new BST node classnewNode:     # Construct to create a new node     def__init__(self, key):         self.key =key        self.left =None        self.right =None# A utility function to do inorder # traversal of BST definorder(root) :    if(root !=None):         inorder(root.left)         printf("%d ", root.key)         inorder(root.right)     """ A utility function to insert a new node with given key in BST """definsert(node, key):     """ If the tree is empty,    return a new node """    if(node ==None):         returnnewNode(key)     """ Otherwise, recur down the tree """    if(key < node.key):         node.left =insert(node.left, key)     else:        node.right =insert(node.right, key)     """ return the (unchanged)         node pointer """    returnnode # Function to print all even nodes defevenNode(root) :    if(root !=None):         evenNode(root.left)                 # if node is even then print it         if(root.key %2==0):            print(root.key, end =" ")         evenNode(root.right) # Driver Code if__name__ =='__main__':        """ Let us create following BST     5     / \     3 7     / \ / \     2 4 6 8 """    root =None    root =insert(root, 5)     root =insert(root, 3)     root =insert(root, 2)     root =insert(root, 4)     root =insert(root, 7)     root =insert(root, 6)     root =insert(root, 8)     evenNode(root) # This code is contributed by# Shubham Singh(SHUBHAMSINGH10) | 
C#
| // C# program to print all even node of BST usingSystem;classGfG {     // create Tree     classNode     {         publicintkey;         publicNode left, right;     }     // A utility function to     // create a new BST node     staticNode newNode(intitem)     {         Node temp = newNode();         temp.key = item;         temp.left = null;         temp.right = null;         returntemp;     }     // A utility function to do    // inorder traversal of BST     staticvoidinorder(Node root)     {         if(root != null)         {             inorder(root.left);             Console.Write(root.key + " ");             inorder(root.right);         }     }     /* A utility function to insert a new node     with given key in BST */    staticNode insert(Node node, intkey)     {         /* If the tree is empty, return a new node */        if(node == null)             returnnewNode(key);         /* Otherwise, recur down the tree */        if(key < node.key)             node.left = insert(node.left, key);         else            node.right = insert(node.right, key);         /* return the (unchanged) node pointer */        returnnode;     }     // Function to print all even nodes     staticvoidevenNode(Node root)     {         if(root != null)        {             evenNode(root.left);                         // if node is even then print it             if(root.key % 2 == 0)                 Console.Write(root.key + " ");             evenNode(root.right);         }     }     // Driver Code     publicstaticvoidMain(String[] args)     {         /* Let us create following BST         5         / \         3 7         / \ / \         2 4 6 8 */        Node root = null;         root = insert(root, 5);         root = insert(root, 3);         root = insert(root, 2);         root = insert(root, 4);         root = insert(root, 7);         root = insert(root, 6);         root = insert(root, 8);         evenNode(root);     } } // This code has been contributed // by PrinciRaj1992 | 
Javascript
| <script>// JavaScript program to print all even node of BST // create Tree      class Node {            constructor() {                this.key = 0;                this.left = null;                this.right = null;            }        }     // A utility function to create a new BST node functionnewNode(item) {     vartemp = newNode();     temp.key = item;     temp.left = null;    temp.right = null;     returntemp; } // A utility function to do inorder traversal of BST functioninorder(root) {     if(root != null) {         inorder(root.left);         document.write(root.key + " ");         inorder(root.right);     } } /* A utility function to insert a new node with given key in BST */functioninsert(node , key) {     /* If the tree is empty, return a new node */    if(node == null)         returnnewNode(key);     /* Otherwise, recur down the tree */    if(key < node.key)         node.left = insert(node.left, key);     else        node.right = insert(node.right, key);     /* return the (unchanged) node pointer */    returnnode; } // Function to print all even nodes functionevenNode(root) {     if(root != null) {         evenNode(root.left);         // if node is even then print it         if(root.key % 2 == 0)             document.write(root.key + " ");         evenNode(root.right);     } } // Driver Code     /* Let us create following BST     5     / \     3     7     / \ / \     2 4 6 8 */    varroot = null;     root = insert(root, 5);     root = insert(root, 3);     root = insert(root, 2);     root = insert(root, 4);     root = insert(root, 7);     root = insert(root, 6);     root = insert(root, 8);     evenNode(root); // This code contributed by aashish1995 </script> | 
2 4 6 8
Complexity Analysis:
- Time Complexity: O(N)
- Here N is the number of nodes and as we have to visit every node the time complexity is O(N).
 
- Auxiliary Space: O(h)
- Here h is the height of the tree and extra space is used in recursion call stack.
 
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!


 
                                    







