-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 1367. Linked List in Binary Tree (#578)
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |