I'm searching a binary tree for a value I know exists, but the method keeps returning null. Print statements show it *reaches* the right node, so I think the problem is how I'm returning the recursive call.
```java
if (node.val == target) return node;
search(node.left, target);
search(node.right, target);
return null;
```
Accepted answer
You find the node in the recursive calls, then throw the result away. `search(node.left, target)` hands a node back, but nothing captures or returns it, so the method falls through to the final `return null`.
Carry the result up instead:
```java
if (node.val == target) return node;
TreeNode left = search(node.left, target);
if (left != null) return left;
return search(node.right, target);
```
Rule of thumb: a recursive method is only as useful as the value it returns. A bare `search(...)` sitting on its own line is always a bug.