-
Notifications
You must be signed in to change notification settings - Fork 1
/
Leaf_at_Same_Level.cpp
76 lines (62 loc) · 1.59 KB
/
Leaf_at_Same_Level.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Problem Statement:
-----------------
Given a Binary Tree, check if all leaves are at same level or not.
Example 1:
---------
Input:
1
/ \
2 3
Output: 1
Explanation: Leaves 2 and 3 are at same level.
Example 2:
---------
Input:
10
/ \
20 30
/ \
10 15
Output: 0
Explanation: Leaves 10, 15 and 30 are not at same level.
Your Task: You dont need to read input or print anything. Complete the function check() which takes root node as input parameter
and returns true/false depending on whether all the leaf nodes are at the same level or not.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(height of tree)
*/
// Link --> https://practice.geeksforgeeks.org/problems/leaf-at-same-level/1#
// Code:
class Solution
{
public:
/*You are required to complete this method*/
int maxLevel = 0;
bool checkUtil(Node *root , int level)
{
if(root == NULL)
return true;
bool answer = true;
if(root->left == NULL and root->right == NULL)
{
if(maxLevel == 0)
{
maxLevel = level;
}
else if(maxLevel != level)
{
return false;
}
}
return(checkUtil(root->left , level+1) && checkUtil(root->right , level+1));
}
bool check(Node *root)
{
if(root == NULL)
return true;
if(checkUtil(root , 0))
return true;
else
return false;
}
};