Binary Tree Node Presence Problem

Determine if a node with a given integer value X exists in a specified binary tree.

Input:

The first line of each test case contains the binary tree nodes in level order sequence as integers, with `-1` representing a null node.
The second line contains the integer X you have to search for in the tree.

Output:

Return 'true' if a node with value X exists in the binary tree, otherwise return 'false'.

Example:

Input:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
5
Output:
true

Constraints:

  • 1 <= N <= 10^5
  • Where N is the total number of nodes in the binary tree.
  • Time Limit: 1 sec.

Note:

You do not need to print anything explicitly in your solution code.

AnswerBot
4mo

Check if a node with a given value exists in a binary tree.

  • Traverse the binary tree using depth-first search (DFS) or breadth-first search (BFS) to search for the node with the given value.

  • Use a recur...read more

Help your peers!
Select
Add answer anonymously...

Maersk Software Developer interview questions & answers

A Software Developer was asked 12mo agoQ. What is a decorator in Python?
A Software Developer was asked Q. Architecture of current project
A Software Developer was asked Q. Shortest Path in an Unweighted Graph The city of Ninjaland is represented as an ...read more

Popular interview questions of Software Developer

A Software Developer was asked 12mo agoQ1. What is a decorator in Python?
A Software Developer was asked Q2. Architecture of current project
A Software Developer was asked Q3. Shortest Path in an Unweighted Graph The city of Ninjaland is represented as an ...read more
Maersk Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits