1+ /*
2+ * @Author: LetMeFly
3+ * @Date: 2024-12-30 13:32:37
4+ * @LastEditors: LetMeFly.xyz
5+ * @LastEditTime: 2024-12-30 14:05:37
6+ */
7+ #ifdef _WIN32
8+ #include " _[1,2]toVector.h"
9+ #endif
10+
11+ /* *
12+ * Definition for singly-linked list.
13+ * struct ListNode {
14+ * int val;
15+ * ListNode *next;
16+ * ListNode() : val(0), next(nullptr) {}
17+ * ListNode(int x) : val(x), next(nullptr) {}
18+ * ListNode(int x, ListNode *next) : val(x), next(next) {}
19+ * };
20+ */
21+ /* *
22+ * Definition for a binary tree node.
23+ * struct TreeNode {
24+ * int val;
25+ * TreeNode *left;
26+ * TreeNode *right;
27+ * TreeNode() : val(0), left(nullptr), right(nullptr) {}
28+ * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
29+ * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
30+ * };
31+ */
32+ // class Solution {
33+ // private:
34+ // bool dfs(ListNode* head, ListNode* now, TreeNode* root) {
35+ // if (!now) {
36+ // return true;
37+ // }
38+ // if (!root) {
39+ // return false;
40+ // }
41+ // if (now->val == root->val) {
42+ // if (dfs(head, now->next, root->left) || dfs(head, now->next, root->right)) {
43+ // return true;
44+ // }
45+ // }
46+ // if (head != now) {
47+ // if (dfs(head, head, root)) {
48+ // return true;
49+ // }
50+ // }
51+ // return dfs(head, now, root->left) || dfs(head, now, root->right);
52+ // }
53+ // public:
54+ // bool isSubPath(ListNode* head, TreeNode* root) {
55+ // return dfs(head, head, root);
56+ // }
57+ // };
58+ class Solution {
59+ private:
60+ bool dfs (ListNode* head, ListNode* l, TreeNode* t) {
61+ if (!l) {
62+ return true ;
63+ }
64+ if (!t) {
65+ return false ;
66+ }
67+ return l->val == t->val && (dfs (head, l->next , t->left ) || dfs (head, l->next , t->right )) ||
68+ head == l && (dfs (head, l, t->left ) || dfs (head, l, t->right ));
69+ }
70+ public:
71+ bool isSubPath (ListNode* head, TreeNode* root) {
72+ return dfs (head, head, root);
73+ }
74+ };
0 commit comments