Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Leaves One Level Apart

Moderate
0/80
Average time to solve is 40m
profile
Contributed by
7 upvotes
Asked in company
Amazon

Problem statement

You are given a binary tree of 'N' nodes. Your task is to print 'True' if the leaves of the tree are at most one level apart else print 'False'.

Detailed explanation ( Input/output format, Notes, Images )
Constraints :
1 <= T <= 2*10^2
0 <= N <= 3*10^3
0 <= NODE.DATA <= 10^5

Time Limit : 1 sec
Sample Input 1 :
1
5 6 2 4 3 -1 -1 9 7 -1 -1 -1 -1 -1 -1
Sample Output 1 :
False
Explanation For Sample Input 1 :

Sample Input 1

The leaves (2, 7) and (2, 9) are two levels apart, thus the answer is False.
Sample Input 2 :
2
2 3 4 -1 -1 -1 -1
8 -1 -1
Sample Output 2 :
True
True
Full screen
Console