-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution.cpp
47 lines (42 loc) · 1.1 KB
/
Solution.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
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
bool isSubPath(ListNode* head, TreeNode* root) {
if ( head == NULL ) return true;
if ( root == NULL) return false;
if ( head->val == root->val and this->con(head, root) )
return true;
auto l = this->isSubPath(head, root->left);
auto r = this->isSubPath(head, root->right);
return l or r;
}
bool con(ListNode* head, TreeNode* root) { //consecutive
if ( head == NULL ) return true;
if ( root == NULL) return false;
if ( head->val == root->val ) {
auto l = this->con(head->next, root->left);
auto r = this->con(head->next, root->right);
return l or r;
}
return false;
}
};
int main() {
Solution s;
}