-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path08.02.2024.cpp
53 lines (45 loc) · 1.17 KB
/
08.02.2024.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
/* The structure of the binary tree is as follows
struct Node
{
int data;
Node* left;
Node* right;
};
*/
class Solution{
public:
/*You are required to complete this method*/
bool check(Node *root)
{
//Your code here
if(root==NULL) return true;
queue<Node* > q;
q.push(root);
int level=0;
int leaf=-1;
while(!q.empty()){
int n=q.size();
level++;
for(int i=0;i<n;i++){
Node* curr=q.front();
q.pop();
if(!curr->left && !curr->right){
//i have encountered a leaf node
if(leaf==-1){
leaf=level;
}
else if(leaf!=level){
return false;
}
}
if(curr->left){
q.push(curr->left);
}
if(curr->right){
q.push(curr->right);
}
}
}
return true;
}
};