VOOZH about

URL: https://www.geeksforgeeks.org/dsa/search-a-node-in-binary-tree/

⇱ Search a node in Binary Tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Search a node in Binary Tree

Last Updated : 11 Jul, 2025

Given a Binary tree and a key. The task is to search and check if the given key exists in the binary tree or not.

Examples:

Input:

👁 search-a-node-in-binary-tree


Output: True

Input:

👁 search-a-node-in-binary-tree-2

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: 


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.

Comment