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.

Be the first one to answer
Add answer anonymously...
Maersk Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter