Given a Binary tree and a key. The task is to search and check if the given keyexists in the binary tree or not.
Examples:
Input:
Output: True
Input:
Output: False
Approach:
The idea is to use any of the tree traversals to traverse the tree and while traversing check if the current node matches with the given node, return true if any node matches with the given node and stop traversing further and if the tree is completely traversed and none of the node matches with the given node then return False.
Below is the implementation of the above approach:
C++
// C++ program to check if a node exists// in a binary tree#include<iostream>usingnamespacestd;classNode{public:intdata;Node*left,*right;Node(intx){data=x;left=nullptr;right=nullptr;}};// Function to traverse the tree in preorder// and check if the given node exists in itboolifNodeExists(Node*root,intkey){if(root==nullptr)returnfalse;if(root->data==key)returntrue;// then recur on left subtreeboolres1=ifNodeExists(root->left,key);// node found, no need to look furtherif(res1)returntrue;// node is not found in left, // so recur on right subtreeboolres2=ifNodeExists(root->right,key);returnres2;}intmain(){// Binary tree // 0// / \ // 1 2// / \ / \ // 3 4 5 6// / / \ // 7 8 9Node*root=newNode(0);root->left=newNode(1);root->left->left=newNode(3);root->left->left->left=newNode(7);root->left->right=newNode(4);root->left->right->left=newNode(8);root->left->right->right=newNode(9);root->right=newNode(2);root->right->left=newNode(5);root->right->right=newNode(6);intkey=4;if(ifNodeExists(root,key))cout<<"True";elsecout<<"False";return0;}
C
// C program to check if a node exists// in a binary tree#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*left,*right;};// Function to traverse the tree in preorder// and check if the given node exists in itintifNodeExists(structNode*root,intkey){if(root==NULL)return0;if(root->data==key)return1;// then recur on left subtreeintres1=ifNodeExists(root->left,key);// node found, no need to look furtherif(res1)return1;// node is not found in left, // so recur on right subtreeintres2=ifNodeExists(root->right,key);returnres2;}structNode*createNode(intx){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=x;newNode->left=NULL;newNode->right=NULL;returnnewNode;}intmain(){// Binary tree // 0// / \ // 1 2// / \ / \ // 3 4 5 6// / / \ // 7 8 9structNode*root=createNode(0);root->left=createNode(1);root->left->left=createNode(3);root->left->left->left=createNode(7);root->left->right=createNode(4);root->left->right->left=createNode(8);root->left->right->right=createNode(9);root->right=createNode(2);root->right->left=createNode(5);root->right->right=createNode(6);intkey=4;if(ifNodeExists(root,key))printf("True");elseprintf("False");return0;}
Java
// Java program to check if a node exists// in a binary treeclassNode{intdata;Nodeleft,right;Node(intx){data=x;left=null;right=null;}}classGfG{// Function to traverse the tree in preorder// and check if the given node exists in itstaticbooleanifNodeExists(Noderoot,intkey){if(root==null)returnfalse;if(root.data==key)returntrue;// then recur on left subtreebooleanres1=ifNodeExists(root.left,key);// node found, no need to look furtherif(res1)returntrue;// node is not found in left,// so recur on right subtreebooleanres2=ifNodeExists(root.right,key);returnres2;}publicstaticvoidmain(String[]args){// Binary tree// 0// / \// 1 2// / \ / \// 3 4 5 6// / / \// 7 8 9Noderoot=newNode(0);root.left=newNode(1);root.left.left=newNode(3);root.left.left.left=newNode(7);root.left.right=newNode(4);root.left.right.left=newNode(8);root.left.right.right=newNode(9);root.right=newNode(2);root.right.left=newNode(5);root.right.right=newNode(6);intkey=4;if(ifNodeExists(root,key))System.out.println("True");elseSystem.out.println("False");}}
Python
# Python program to check if a node exists# in a binary treeclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=None# Function to traverse the tree in preorder# and check if the given node exists in itdefifNodeExists(root,key):ifrootisNone:returnFalseifroot.data==key:returnTrue# then recur on left subtreeres1=ifNodeExists(root.left,key)# node found, no need to look furtherifres1:returnTrue# node is not found in left,# so recur on right subtreeres2=ifNodeExists(root.right,key)returnres2if__name__=="__main__":# Binary tree # 0# / \# 1 2# / \ / \# 3 4 5 6# / / \# 7 8 9root=Node(0)root.left=Node(1)root.left.left=Node(3)root.left.left.left=Node(7)root.left.right=Node(4)root.left.right.left=Node(8)root.left.right.right=Node(9)root.right=Node(2)root.right.left=Node(5)root.right.right=Node(6)key=4ififNodeExists(root,key):print("True")else:print("False")
C#
// C# program to check if a node exists// in a binary treeclassNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=null;right=null;}}classGfG{// Function to traverse the tree in preorder// and check if the given node exists in itstaticboolifNodeExists(Noderoot,intkey){if(root==null)returnfalse;if(root.data==key)returntrue;// then recur on left subtreeboolres1=ifNodeExists(root.left,key);// node found, no need to look furtherif(res1)returntrue;// node is not found in left,// so recur on right subtreeboolres2=ifNodeExists(root.right,key);returnres2;}staticvoidMain(string[]args){// Binary tree// 0// / \// 1 2// / \ / \// 3 4 5 6// / / \// 7 8 9Noderoot=newNode(0);root.left=newNode(1);root.left.left=newNode(3);root.left.left.left=newNode(7);root.left.right=newNode(4);root.left.right.left=newNode(8);root.left.right.right=newNode(9);root.right=newNode(2);root.right.left=newNode(5);root.right.right=newNode(6);intkey=4;if(ifNodeExists(root,key))System.Console.WriteLine("True");elseSystem.Console.WriteLine("False");}}
JavaScript
// JavaScript program to check if a node exists// in a binary treeclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}// Function to traverse the tree in preorder// and check if the given node exists in itfunctionifNodeExists(root,key){if(root===null)returnfalse;if(root.data===key)returntrue;// then recur on left subtreeletres1=ifNodeExists(root.left,key);// node found, no need to look furtherif(res1)returntrue;// node is not found in left,// so recur on right subtreeletres2=ifNodeExists(root.right,key);returnres2;}// Binary tree// 0// / \// 1 2// / \ / \// 3 4 5 6// / / \// 7 8 9letroot=newNode(0);root.left=newNode(1);root.left.left=newNode(3);root.left.left.left=newNode(7);root.left.right=newNode(4);root.left.right.left=newNode(8);root.left.right.right=newNode(9);root.right=newNode(2);root.right.left=newNode(5);root.right.right=newNode(6);letkey=4;if(ifNodeExists(root,key))console.log("True");elseconsole.log("False");
Output
True
Time Complexity: O(n), where n is the number of nodes in the tree. Auxiliary Space: O(h), where h is the height of the tree.