Forgetting null/base-case handling
Wrong move: Recursive traversal assumes children always exist.
Usually fails on: Leaf nodes throw errors or create wrong depth/path values.
Fix: Handle null/base cases before recursive transitions.
Build confidence with an intuition-first walkthrough focused on tree fundamentals.
You are given the root of a binary search tree (BST) and an integer val.
Find the node in the BST that the node's value equals val and return the subtree rooted with that node. If such a node does not exist, return null.
Example 1:
Input: root = [4,2,7,1,3], val = 2 Output: [2,1,3]
Example 2:
Input: root = [4,2,7,1,3], val = 5 Output: []
Constraints:
[1, 5000].1 <= Node.val <= 107root is a binary search tree.1 <= val <= 107Problem summary: You are given the root of a binary search tree (BST) and an integer val. Find the node in the BST that the node's value equals val and return the subtree rooted with that node. If such a node does not exist, return null.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Tree
[4,2,7,1,3] 2
[4,2,7,1,3] 5
closest-binary-search-tree-value)insert-into-a-binary-search-tree)closest-nodes-queries-in-a-binary-search-tree)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #700: Search in a Binary Search Tree
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if (root == null || root.val == val) {
return root;
}
return root.val > val ? searchBST(root.left, val) : searchBST(root.right, val);
}
}
// Accepted solution for LeetCode #700: Search in a Binary Search Tree
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func searchBST(root *TreeNode, val int) *TreeNode {
if root == nil || root.Val == val {
return root
}
if root.Val > val {
return searchBST(root.Left, val)
}
return searchBST(root.Right, val)
}
# Accepted solution for LeetCode #700: Search in a Binary Search Tree
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
if root is None or root.val == val:
return root
return (
self.searchBST(root.left, val)
if root.val > val
else self.searchBST(root.right, val)
)
// Accepted solution for LeetCode #700: Search in a Binary Search Tree
struct Solution;
use rustgym_util::*;
trait Search {
fn find(&self, val: i32) -> TreeLink;
}
impl Search for TreeLink {
fn find(&self, val: i32) -> TreeLink {
if let Some(node) = self {
let temp = node.clone();
let node = node.borrow();
if val == node.val {
Some(temp)
} else {
if val < node.val {
Self::find(&node.left, val)
} else {
Self::find(&node.right, val)
}
}
} else {
None
}
}
}
impl Solution {
fn search_bst(root: TreeLink, val: i32) -> TreeLink {
root.find(val)
}
}
#[test]
fn test() {
let root = tree!(4, tree!(2, tree!(1), tree!(3)), tree!(3));
let res = tree!(2, tree!(1), tree!(3));
assert_eq!(Solution::search_bst(root, 2), res);
}
// Accepted solution for LeetCode #700: Search in a Binary Search Tree
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function searchBST(root: TreeNode | null, val: number): TreeNode | null {
if (root === null || root.val === val) {
return root;
}
return root.val > val ? searchBST(root.left, val) : searchBST(root.right, val);
}
Use this to step through a reusable interview workflow for this problem.
BFS with a queue visits every node exactly once — O(n) time. The queue may hold an entire level of the tree, which for a complete binary tree is up to n/2 nodes = O(n) space. This is optimal in time but costly in space for wide trees.
Every node is visited exactly once, giving O(n) time. Space depends on tree shape: O(h) for recursive DFS (stack depth = height h), or O(w) for BFS (queue width = widest level). For balanced trees h = log n; for skewed trees h = n.
Review these before coding to avoid predictable interview regressions.
Wrong move: Recursive traversal assumes children always exist.
Usually fails on: Leaf nodes throw errors or create wrong depth/path values.
Fix: Handle null/base cases before recursive transitions.