Member-only story

Search In a Binary Search Tree 🌳| Daily LeetCode Challenge | Day 14 | Coding Interview

Ganesh Prasad
2 min readApr 14, 2022

--

LeetCode 700 | LeetCode daily challenge Day 14 Solution

This is the 14th problem of the challenge and it's just a simple BST traversal.

Description

We are given a BST and an integer, we have to find the value in the BST and return the root of the subtree holding the node with the passed value. Return null if the passed value does not exist.

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: []

Solution for the search in BST

We are given the root of the tree, first, we will find the node in the left subtree and then in the right subtree, and finally, look at the current node if it holds the value equal to our passed parameter.

The algorithm for searching

  1. Check if the root is null, if it is then return null, showing the value does not exist.
  2. Call the function recursively on the left…

--

--

Ganesh Prasad
Ganesh Prasad

Written by Ganesh Prasad

Backend Developer at Appscrip | C++ veteran, 💜 Dart

No responses yet