-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathequal-paths.cpp
34 lines (26 loc) · 915 Bytes
/
equal-paths.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
#ifndef RECCHECK
//if you want to add any #includes like <iostream> you must do them here (before the next endif)
#endif
#include "equal-paths.h"
using namespace std;
// You may add any prototypes of helper functions here
bool isLeaf(Node* root){
return (not root->left and not root->right);
}
bool equalPaths(Node * root)
{
// Add your code below
if(!root) return true;
// idea: if all leaves are on the same level, return true
// in other words, if there exists a leaf on one end and a non-leaf on the other, return false
if(root->left and root->right){
if(isLeaf(root->left) and not isLeaf(root->right) or not isLeaf(root->left) and isLeaf(root->right)) return false;
return equalPaths(root->left) and equalPaths(root->right);
} else if(root->left){
return equalPaths(root->left);
} else if(root->right){
return equalPaths(root->right);
} else{
return true;
}
}