Code Logo

BST Search

Published at25 Jul 2026
Binary Search Tree Easy 0 views
Like0

Given a sorted array representing a BST (inorder traversal), check if a target value exists in the tree. Since the array IS the inorder traversal of a BST, we can simply check if the target exists in the array.

For a BST, searching can also be done using binary search on the sorted array for O(log n) time instead of O(n).

Example Input & Output

Example 1
Input
[1,2,3,4,5],6
Output
false
Explanation

6 not in tree

Example 2
Input
[],5
Output
false
Explanation

Empty tree

Example 3
Input
[1,2,3,4,5],3
Output
true
Explanation

3 exists

Example 4
Input
[5],5
Output
true
Explanation

Root match

Example 5
Input
[10,20,30],15
Output
false
Explanation

Not found

Algorithm Flow

Recommendation Algorithm Flow for BST Search
Recommendation Algorithm Flow for BST Search

Solution Approach

function solution(arr, target) {
  return arr.indexOf(target) !== -1;
}

Best Answers

java
class Solution {
    public boolean solution(int[] nums, int t) {
        for(int n:nums)if(n==t)return true;return false;
    }
}