Skip to content

Commit

Permalink
Create 1367. Linked List in Binary Tree (#578)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chayandas07 authored Sep 7, 2024
2 parents 63d9528 + 1a9a451 commit b44a384
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions 1367. Linked List in Binary Tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
public:
// check all the corresponding downward values and return true if they are the same
bool comparison(TreeNode* root, ListNode* head) {
if(!head) return true;
if(!root) return false;
if(root->val != head->val) return false;

return comparison(root->left, head->next) || comparison(root->right, head->next);
}

bool res = false;

//traverse all nodes in tree to check if the node value is equal to list's head
void inOrder(TreeNode* root, ListNode* head) {
if(!root) return;

//if the values are equal, then check the next values
if(root->val == head->val) {
res = comparison(root, head);
}
if(res) return;
inOrder(root->left, head);
if(res) return;
inOrder(root->right, head);
if(res) return;
}
bool isSubPath(ListNode* head, TreeNode* root) {

inOrder(root, head);
return res;
}
};

0 comments on commit b44a384

Please sign in to comment.