866. Find Level of a Node in Binary Tree

EasyTreesBinary TreeBFS

Given the root of a binary tree and a target value, return the level of the node holding that value, where the root is at level 1. Return -1 if the value is not present. The input gives a level-order tree array and the target separated by ' | '.

Input: A level-order tree array and the target, separated by ' | '.

Output: Integer — the 1-indexed level, or -1.

Examples

Example 1
Input: [1,2,3,4,5,null,7] | 5
Output: 3
Explanation: 5 sits on the third level.
Example 2
Input: [1,2,3] | 3
Output: 2
Explanation: 3 is a child of the root.
Example 3
Input: [1,2,3] | 9
Output: -1
Explanation: 9 is absent.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →