🧠
Brute Force (Recursive Full Tree Traversal)
💡 This approach exists to build intuition by ignoring BST properties and searching every node, helping beginners understand the problem structure before optimization.
Intuition
Traverse the entire tree recursively and check each node's value against the target. Return the node if found, else continue searching both left and right subtrees.
Algorithm
- Start at the root node.
- If the current node is null, return null (not found).
- If the current node's value equals the target, return the current node.
- Recursively search the left subtree; if found, return the result.
- Otherwise, recursively search the right subtree and return the result.
💡 This approach is straightforward but inefficient because it does not use the BST property to skip subtrees.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def searchBST(root, val):
if root is None:
return None
if root.val == val:
return root
left_search = searchBST(root.left, val)
if left_search is not None:
return left_search
return searchBST(root.right, val)
# Example usage:
# Construct BST: 4
# / \
# 2 7
# / \
# 1 3
root = TreeNode(4, TreeNode(2, TreeNode(1), TreeNode(3)), TreeNode(7))
result = searchBST(root, 2)
print(result.val if result else 'null')
Line Notes
if root is None:Base case: if node is null, target not found here
if root.val == val:Check if current node matches target
left_search = searchBST(root.left, val)Search left subtree recursively
if left_search is not None:If found in left subtree, return immediately
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int val) { this.val = val; }
}
public class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if (root == null) return null;
if (root.val == val) return root;
TreeNode leftSearch = searchBST(root.left, val);
if (leftSearch != null) return leftSearch;
return searchBST(root.right, val);
}
public static void main(String[] args) {
TreeNode root = new TreeNode(4);
root.left = new TreeNode(2);
root.right = new TreeNode(7);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(3);
Solution sol = new Solution();
TreeNode res = sol.searchBST(root, 2);
System.out.println(res != null ? res.val : "null");
}
}
Line Notes
if (root == null) return null;Base case: no node means target not found
if (root.val == val) return root;Check current node for target
TreeNode leftSearch = searchBST(root.left, val);Search left subtree recursively
if (leftSearch != null) return leftSearch;Return immediately if found in left subtree
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if (!root) return nullptr;
if (root->val == val) return root;
TreeNode* leftSearch = searchBST(root->left, val);
if (leftSearch) return leftSearch;
return searchBST(root->right, val);
}
};
int main() {
TreeNode* root = new TreeNode(4);
root->left = new TreeNode(2);
root->right = new TreeNode(7);
root->left->left = new TreeNode(1);
root->left->right = new TreeNode(3);
Solution sol;
TreeNode* res = sol.searchBST(root, 2);
cout << (res ? to_string(res->val) : "null") << endl;
return 0;
}
Line Notes
if (!root) return nullptr;Base case: no node means target not found
if (root->val == val) return root;Check current node for target
TreeNode* leftSearch = searchBST(root->left, val);Search left subtree recursively
if (leftSearch) return leftSearch;Return immediately if found in left subtree
class TreeNode {
constructor(val=0, left=null, right=null) {
this.val = val;
this.left = left;
this.right = right;
}
}
function searchBST(root, val) {
if (root === null) return null;
if (root.val === val) return root;
const leftSearch = searchBST(root.left, val);
if (leftSearch !== null) return leftSearch;
return searchBST(root.right, val);
}
// Example usage:
const root = new TreeNode(4, new TreeNode(2, new TreeNode(1), new TreeNode(3)), new TreeNode(7));
const result = searchBST(root, 2);
console.log(result ? result.val : 'null');
Line Notes
if (root === null) return null;Base case: no node means target not found
if (root.val === val) return root;Check current node for target
const leftSearch = searchBST(root.left, val);Search left subtree recursively
if (leftSearch !== null) return leftSearch;Return immediately if found in left subtree
In worst case, we visit every node (n nodes). The recursion stack can go as deep as the tree height h.
💡 For n=20 nodes, this means up to 20 checks and recursion depth up to 20 in worst case.
Interview Verdict: Accepted but inefficient
This approach works but ignores BST properties, so it is slower than necessary.