nikunjsoni

1367

Mar 19th, 2021
80
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Definition for singly-linked list.
  3.  * struct ListNode {
  4.  *     int val;
  5.  *     ListNode *next;
  6.  *     ListNode() : val(0), next(nullptr) {}
  7.  *     ListNode(int x) : val(x), next(nullptr) {}
  8.  *     ListNode(int x, ListNode *next) : val(x), next(next) {}
  9.  * };
  10.  */
  11. /**
  12.  * Definition for a binary tree node.
  13.  * struct TreeNode {
  14.  *     int val;
  15.  *     TreeNode *left;
  16.  *     TreeNode *right;
  17.  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
  18.  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  19.  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  20.  * };
  21.  */
  22. class Solution {
  23. public:
  24.     bool checkNode(ListNode* head, TreeNode* root){
  25.         if(!head) return true;
  26.         if(!root) return false;
  27.         return (head->val == root->val) && (checkNode(head->next, root->left) || checkNode(head->next, root->right));
  28.     }
  29.     bool isSubPath(ListNode* head, TreeNode* root) {
  30.         if(!head) return true;
  31.         if(!root) return false;
  32.         return (checkNode(head, root) || isSubPath(head, root->left) || isSubPath(head, root->right));
  33.     }
  34. };
RAW Paste Data