# Search a node in Binary Tree

Given a Binary Node and a node. The task is to search and check if the given node exits 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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

 `// C++ program to check if a node exists ` `// in a binary tree ` `#include ` `using` `namespace` `std; ` ` `  `// Binary tree node ` `struct` `Node { ` `    ``int` `data; ` `    ``struct` `Node *left, *right; ` `    ``Node(``int` `data) ` `    ``{ ` `        ``this``->data = data; ` `        ``left = right = NULL; ` `    ``} ` `}; ` ` `  `// Function to traverse the tree in preorder ` `// and check if the given node exists in it ` `bool` `ifNodeExists(``struct` `Node* node, ``int` `key) ` `{ ` `    ``if` `(node == NULL) ` `        ``return` `false``; ` ` `  `    ``if` `(node->data == key) ` `        ``return` `true``; ` ` `  `    ``/* then recur on left sutree */` `    ``bool` `res1 = ifNodeExists(node->left, key); ` ` `  `    ``/* now recur on right subtree */` `    ``bool` `res2 = ifNodeExists(node->right, key); ` ` `  `    ``return` `res1 || res2; ` `} ` ` `  `// Driver Code ` `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; ` `} `

## Python3

“””Python program to check if a node exists
in a binary tree.”””

# A Binary Tree Node
# Utility function to create a new tree node
class newNode:

# Constructor to create a newNode
def __init__(self, data):
self.data = data
self.left = None
self.right = None

# Function to traverse the tree in preorder
# and check if the given node exists in it
def ifNodeExists(node, key):

if (node == None):
return False

if (node.data == key):
return True

“”” then recur on left sutree “””
res1 = ifNodeExists(node.left, key)

“”” now recur on right subtree “””
res2 = ifNodeExists(node.right, key)

return res1 or res2

# Driver Code
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”)

# This code is contributed by SHUBHAMSINGH10

Output:

```YES
```

My Personal Notes arrow_drop_up Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : SHUBHAMSINGH10

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.