File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments