Skip to content

Commit b44a384

Browse files
authored
Create 1367. Linked List in Binary Tree (#578)
2 parents 63d9528 + 1a9a451 commit b44a384

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

1367. Linked List in Binary Tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
// check all the corresponding downward values and return true if they are the same
4+
bool comparison(TreeNode* root, ListNode* head) {
5+
if(!head) return true;
6+
if(!root) return false;
7+
if(root->val != head->val) return false;
8+
9+
return comparison(root->left, head->next) || comparison(root->right, head->next);
10+
}
11+
12+
bool res = false;
13+
14+
//traverse all nodes in tree to check if the node value is equal to list's head
15+
void inOrder(TreeNode* root, ListNode* head) {
16+
if(!root) return;
17+
18+
//if the values are equal, then check the next values
19+
if(root->val == head->val) {
20+
res = comparison(root, head);
21+
}
22+
if(res) return;
23+
inOrder(root->left, head);
24+
if(res) return;
25+
inOrder(root->right, head);
26+
if(res) return;
27+
}
28+
bool isSubPath(ListNode* head, TreeNode* root) {
29+
30+
inOrder(root, head);
31+
return res;
32+
}
33+
};

0 commit comments

Comments
 (0)