Search a node in Binary Tree
Last Updated :
08 Sep, 2022
Given a Binary tree and a node. The task is to search and check if the given node exists in the binary tree or not. If it exists, print YES otherwise print NO.
Given Binary Tree:
Examples:
Input: Node = 4
Output: YES
Input: Node = 40
Output: NO
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. Print YES 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 print NO.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
Node( int data)
{
this ->data = data;
left = right = NULL;
}
};
bool ifNodeExists( struct Node* node, int key)
{
if (node == NULL)
return false ;
if (node->data == key)
return true ;
bool res1 = ifNodeExists(node->left, key);
if (res1) return true ;
bool res2 = ifNodeExists(node->right, key);
return res2;
}
int main()
{
struct Node* root = new Node(0);
root->left = new Node(1);
root->left->left = new Node(3);
root->left->left->left = new Node(7);
root->left->right = new Node(4);
root->left->right->left = new Node(8);
root->left->right->right = new Node(9);
root->right = new Node(2);
root->right->left = new Node(5);
root->right->right = new Node(6);
int key = 4;
if (ifNodeExists(root, key))
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node left, right;
Node( int data)
{
this .data = data;
left = right = null ;
}
};
static boolean ifNodeExists( Node node, int key)
{
if (node == null )
return false ;
if (node.data == key)
return true ;
boolean res1 = ifNodeExists(node.left, key);
if (res1) return true ;
boolean res2 = ifNodeExists(node.right, key);
return res2;
}
public static void main(String args[])
{
Node root = new Node( 0 );
root.left = new Node( 1 );
root.left.left = new Node( 3 );
root.left.left.left = new Node( 7 );
root.left.right = new Node( 4 );
root.left.right.left = new Node( 8 );
root.left.right.right = new Node( 9 );
root.right = new Node( 2 );
root.right.left = new Node( 5 );
root.right.right = new Node( 6 );
int key = 4 ;
if (ifNodeExists(root, key))
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
class newNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def ifNodeExists(node, key):
if (node = = None ):
return False
if (node.data = = key):
return True
res1 = ifNodeExists(node.left, key)
if res1:
return True
res2 = ifNodeExists(node.right, key)
return res2
if __name__ = = '__main__' :
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 )
key = 4
if (ifNodeExists(root, key)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node left, right;
public Node( int data)
{
this .data = data;
left = right = null ;
}
};
static bool ifNodeExists( Node node, int key)
{
if (node == null )
return false ;
if (node.data == key)
return true ;
bool res1 = ifNodeExists(node.left, key);
if (res1) return true ;
bool res2 = ifNodeExists(node.right, key);
return res2;
}
public static void Main(String []args)
{
Node root = new Node(0);
root.left = new Node(1);
root.left.left = new Node(3);
root.left.left.left = new Node(7);
root.left.right = new Node(4);
root.left.right.left = new Node(8);
root.left.right.right = new Node(9);
root.right = new Node(2);
root.right.left = new Node(5);
root.right.right = new Node(6);
int key = 4;
if (ifNodeExists(root, key))
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
Javascript
<script>
class Node {
constructor(data) {
this .data = data;
this .left = this .right = null ;
}
}
function ifNodeExists(node , key) {
if (node == null )
return false ;
if (node.data == key)
return true ;
var res1 = ifNodeExists(node.left, key);
if (res1)
return true ;
var res2 = ifNodeExists(node.right, key);
return res2;
}
var root = new Node(0);
root.left = new Node(1);
root.left.left = new Node(3);
root.left.left.left = new Node(7);
root.left.right = new Node(4);
root.left.right.left = new Node(8);
root.left.right.right = new Node(9);
root.right = new Node(2);
root.right.left = new Node(5);
root.right.right = new Node(6);
var key = 4;
if (ifNodeExists(root, key))
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Complexity Analysis:
- Time Complexity: O(N), as we are using recursion to traverse N nodes of the tree.
- Auxiliary Space: O(N), we are not using any explicit extra space but as we are using recursion there will be extra space allocated for recursive stack.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...