Special Binary Tree.

Easy
0/40
Average time to solve is 15m
profile
Contributed by
34 upvotes
Asked in companies
AmazonWipro

Problem statement

You are given an arbitrary binary tree. A binary tree is called special if every node of this tree has either zero or two children. You have to determine if the given binary tree is special or not.

If the given binary tree is special, return True. Else, return False to the given function.

Note:

1. A binary tree is a tree in which each node can have at most two children. 
2. The given tree will be non-empty i.e the number of non-NULL nodes will always be greater than or equal to 1.
3. Multiple nodes in the tree can have the same values, all values in the tree will be positive.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of the input contains an integer ‘T’, denoting the number of test cases. 

The first line of every test case will contain the values of the nodes of the binary tree in the level order form ( -1 for NULL node).  Refer to the example below for further explanation.
For Example:
Consider the binary tree:

The input for the tree depicted in the above image would be :

3
5 1
6 2 0 8
-1 -1 7 4 -1 -1 -1 -1
-1 -1 -1 -1

Explanation :
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)

The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.

The input ends when all nodes at the last level are null (-1).
Output Format :
For each test case, print a single line containing “true” or “false”.

The output of each test case will be printed in a separate line.

Note :

You do not need to print anything, it has already been taken care of. Just implement the given function.

Constraints:

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

Where ‘DATA’ denotes the value of each node in the given tree and ‘N’ denotes the number of nodes in BinaryTree.

Time limit: 1 sec
Sample Input 1:
2
3 5 1 6 2 0 8 -1 -1 7 4 -1 -1 -1 -1 -1 -1 -1 -1
1 2 3 4 5 -1 -1 6 -1 -1 -1 -1 -1    
Sample Output 1:
True
False
Explanation for sample input 1:

Test Case1: 

The tree given in Test Case 1, is shown in the image above. Note that nodes with the values 6, 7, 4, 0, 8, are leaf nodes and have no children, other nodes in the tree have two children each. So the given binary tree is special.

Test Case 2:

The tree given in Test Case 2, is shown in the image above. Note that node with the value 4, has only one child, thus the given binary tree is not special.
Sample Input 2:
2
5 2 3 8 1 -1 -1 7 9 -1 -1 5 6 -1 -1 -1 -1 -1 -1
1 5 7 -1 -1 6 3 9 8 -1 -1 -1 -1 13 -1 -1 -1 
Sample Output 2:
True
False
Hint

Try to think of a recursive solution.

Approaches (2)
Recursion.

The idea is to check for each node of the binary tree whether the node has 0 child or having two children recursively.

 

Approach :

 

  • You are given the root node of a binary tree, you have to tell if the given binary tree is special i.e. each and every node of the binary tree has zero or two children.
  • The problem can be easily thought of using recursion.
  • As we know, recursion has three main sections: Base Case, Recursive Calls and Small Calculation over results from recursion.
  • Let’s identify, the base case for the problem:
    • If we reach a leaf node, then its full binary tree, because its left and right child pointers are pointing to NULL, so it means that it does not have any child node
    • Also, if we reach the NULL node, then its also a special subtree, therefore return “true” to the function.
    • Let’s do the recursive calls:
    • We want to know if a given subtree is special or not.  So if a node has only one child, then clearly the condition is violated, otherwise, we check for special property recursively for both its left and right subtrees.
    • If both the subtrees (left and right) of a node are special, then we can say the subtree rooted at the current node is special as well.
    • So, return “true” to the function.
Time Complexity

O(N), where ‘N’ denotes the number of nodes in the given tree.

 

As we will be checking for each node of the binary tree whether the node contributes to the special tree or not. Therefore, overall time complexity will be O(N).  

Space Complexity

O(N), where ‘N’ denotes the number of nodes in the binary tree.

 

As extra space is used for recursion call stack and as we are having ‘N’ returning values. Therefore, overall space complexity will be O(N).

Code Solution
(100% EXP penalty)
Special Binary Tree.
Full screen
Console