Skip to content

Commit e4e0848

Browse files
committed
Time: N/A (0%), Space: N/A (0%) - LeetHub
1 parent 18c81f9 commit e4e0848

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

1367-linked-list-in-binary-tree/1367-linked-list-in-binary-tree.cpp

+14-7
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,23 @@
2121
*/
2222
class Solution {
2323
public:
24+
ListNode* mainHead;
25+
2426
bool isSubPath(ListNode* head, TreeNode* root) {
25-
if (!root) return false;
26-
return dfs(root, head) || isSubPath(head, root->left) || isSubPath(head, root->right);
27+
mainHead = head;
28+
return solve(root, head);
2729
}
2830

29-
bool dfs(TreeNode* root, ListNode* head) {
30-
if (!head) return true;
31-
if (!root) return false;
32-
if (root->val != head->val) return false;
31+
bool solve(TreeNode* root, ListNode* head) {
32+
if (!head) return true;
33+
if (!root) return false;
34+
35+
if (root->val == head->val) {
36+
if (solve(root->left, head->next) || solve(root->right, head->next)) {
37+
return true;
38+
}
39+
}
3340

34-
return dfs(root->left, head->next) || dfs(root->right, head->next);
41+
return solve(root->left, mainHead) || solve(root->right, mainHead);
3542
}
3643
};

0 commit comments

Comments
 (0)