-
Notifications
You must be signed in to change notification settings - Fork 508
/
Copy pathLeaf at same level.cpp
64 lines (53 loc) · 1.2 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
/*
Leaf at same level
==================
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)
Constraints:
1 ≤ N ≤ 10^3
*/
bool helper(Node *root, int &level, int depth)
{
if (!root->left && !root->right)
{
if (level == -1)
level = depth;
else if (level != depth)
return false;
return true;
}
if (root->left && !helper(root->left, level, depth + 1))
return false;
if (root->right && !helper(root->right, level, depth + 1))
return false;
;
return true;
}
bool check(Node *root)
{
if (!root)
return true;
int level = -1;
return helper(root, level, 0);
}