Print all even nodes of Binary Search Tree

01 Nov, 2021
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++
Java
Python3
C#
Javascript
// C++ program to print all even node of BST#include <bits/stdc++.h>using namespace std; // create Treestruct Node {    int key;    struct Node *left, *right;}; // A utility function to create a new BST nodeNode* newNode(int item){    Node* temp = new Node;    temp->key = item;    temp->left = temp->right = NULL;    return temp;} // A utility function to do inorder traversal of BSTvoid inorder(Node* root){    if (root != NULL) {        inorder(root->left);        printf(root->key,end=" ");        inorder(root->right);    }} /* A utility function to insert a new node   with given key in BST */Node* insert(Node* node, int key){    /* If the tree is empty, return a new node */    if (node == NULL)        return newNode(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 */    return node;} // Function to print all even nodesvoid evenNode(Node* root){    if (root != NULL) {        evenNode(root->left);        // if node is even then print it        if (root->key % 2 == 0)            printf("%d ", root->key);        evenNode(root->right);    }} // Driver Codeint main(){    /* 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);     return 0;}
// Java program to print all even node of BSTclass GfG { // create Treestatic class Node {    int key;    Node left, right;} // A utility function to create a new BST nodestatic Node newNode(int item){    Node temp = new Node();    temp.key = item;    temp.left = null;    temp.right = null;    return temp;} // A utility function to do inorder traversal of BSTstatic void inorder(Node root){    if (root != null) {        inorder(root.left);        System.out.print(root.key + " ");        inorder(root.right);    }} /* A utility function to insert a new nodewith given key in BST */static Node insert(Node node, int key){    /* If the tree is empty, return a new node */    if (node == null)        return newNode(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 */    return node;} // Function to print all even nodesstatic void evenNode(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 Codepublic static void main(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 program to print all even node of BST # create Tree# to create a new BST nodeclass newNode:     # 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 BSTdef inorder(root) :     if (root != None):        inorder(root.left)        printf("%d ", root.key)        inorder(root.right)     """ A utility function to insert a newnode with given key in BST """def insert(node, key):     """ If the tree is empty,    return a new node """    if (node == None):        return newNode(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 """    return node # Function to print all even nodesdef evenNode(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 Codeif __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# program to print all even node of BSTusing System; class GfG{     // create Tree    class Node    {        public int key;        public Node left, right;    }     // A utility function to    // create a new BST node    static Node newNode(int item)    {        Node temp = new Node();        temp.key = item;        temp.left = null;        temp.right = null;        return temp;    }     // A utility function to do    // inorder traversal of BST    static void inorder(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 */    static Node insert(Node node, int key)    {        /* If the tree is empty, return a new node */        if (node == null)            return newNode(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 */        return node;    }     // Function to print all even nodes    static void evenNode(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    public static void Main(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
<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 nodefunction newNode(item){    var temp = new Node();    temp.key = item;    temp.left = null;    temp.right = null;    return temp;} // A utility function to do inorder traversal of BSTfunction inorder(root){    if (root != null) {        inorder(root.left);        document.write(root.key + " ");        inorder(root.right);    }} /* A utility function to insert a new nodewith given key in BST */function insert(node , key){    /* If the tree is empty, return a new node */    if (node == null)        return newNode(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 */    return node;} // Function to print all even nodesfunction evenNode(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 */    var 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 contributed by aashish1995 </script>
2 4 6 8
 
prerna saini
princiraj1992
SHUBHAMSINGH10
nidhi_biet
aashish1995
Kirti_Mangal
BST
Technical Scripter 2018
Tree Traversals
Binary Search Tree
Technical Scripter
Tree
Binary Search Tree
Tree
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Find postorder traversal of BST from preorder traversal
Overview of Data Structures | Set 2 (Binary Tree, BST, Heap and Hash)
Sorted Array to Balanced BST
Optimal Binary Search Tree | DP-24
Lowest Common Ancestor in a Binary Search Tree.
Tree Traversals (Inorder, Preorder and Postorder)
Binary Tree | Set 1 (Introduction)
Level Order Binary Tree Traversal
Introduction to Data Structures
Introduction to Tree Data Structure