Special Binary Tree Problem Statement

Determine whether an arbitrary binary tree is special. A binary tree is called special if every node in this tree has either zero or two children.

Example:

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

Consider the binary tree:

Level 1: The root node of the tree is 3

Level 2: Left child of 3 = 5, Right child of 3 = 1

Level 3: Left child of 5 = 6, Right child of 5 = 2, Left child of 1 = 0, Right child of 1 = 8

Level 4: Left child of 6 = null (-1), Right child of 6 = null (-1), Left child of 2 = 7, Right child of 2 = 4, Left child of 0 = null (-1), Right child of 0 = null (-1), Left child of 8 = null (-1), Right child of 8 = null (-1)

Level 5: Left child of 7 = null (-1), Right child of 7 = null (-1), Left child of 4 = null (-1), Right child of 4 = null (-1)

Input:

The first line contains an integer ‘T’ denoting the number of test cases. For each test case, provide the binary tree's node values in level order (use -1 for NULL nodes).

Output:

For each test case, print either “true” if the tree is special or “false” otherwise. Each result should be on a new line.

Constraints:

  • 1 <= T <= 5
  • 1 <= N <= 10 ^ 3
  • 0 <= DATA <= 10 ^ 9

Note:

  • A binary tree is a tree in which each node can have at most two children.
  • The given tree will always be non-empty with at least one node.
  • Multiple nodes can have the same value, all node values are positive.
  • No code for input/output interactions is needed; focus on implementing the logic for checking if the tree is special.
AnswerBot
5d

Determine if a binary tree is special if every node has zero or two children.

  • Traverse the tree and check if each node has either 0 or 2 children.

  • Use a recursive approach to check children of each node...read more

Help your peers!
Add answer anonymously...
Amazon 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