Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
};
Loading