More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Ans. public static int getLevel(Node root, int key) {
int levelOfNode = getLevelUtil(root, key, 1);
return levelOfNode;
}
public static int getLevelUtil(Node root, int key, int level) {
if (root == null) return 0;
if (root.data == key) return level;
int left = getLevelUtil(root.left, key, level 1);
int right = getLevelUtil(root.right, key, level 1);
if (left != 0) return left;
else return right;
}
Help us improve. Please let us know the company, where you were asked this question :