|
| 1 | +/* |
| 2 | + * @lc app=leetcode id=1367 lang=cpp |
| 3 | + * |
| 4 | + * [1367] Linked List in Binary Tree |
| 5 | + * |
| 6 | + * https://leetcode.com/problems/linked-list-in-binary-tree/description/ |
| 7 | + * |
| 8 | + * algorithms |
| 9 | + * Medium (39.40%) |
| 10 | + * Likes: 181 |
| 11 | + * Dislikes: 9 |
| 12 | + * Total Accepted: 7.2K |
| 13 | + * Total Submissions: 18.2K |
| 14 | + * Testcase Example: '[4,2,8]\n[1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]' |
| 15 | + * |
| 16 | + * Given a binary tree root and a linked list with head as the first node. |
| 17 | + * |
| 18 | + * Return True if all the elements in the linked list starting from the head |
| 19 | + * correspond to some downward path connected in the binary tree otherwise |
| 20 | + * return False. |
| 21 | + * |
| 22 | + * In this context downward path means a path that starts at some node and goes |
| 23 | + * downwards. |
| 24 | + * |
| 25 | + * |
| 26 | + * Example 1: |
| 27 | + * |
| 28 | + * |
| 29 | + * |
| 30 | + * |
| 31 | + * Input: head = [4,2,8], root = |
| 32 | + * [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] |
| 33 | + * Output: true |
| 34 | + * Explanation: Nodes in blue form a subpath in the binary Tree. |
| 35 | + * |
| 36 | + * |
| 37 | + * Example 2: |
| 38 | + * |
| 39 | + * |
| 40 | + * |
| 41 | + * |
| 42 | + * Input: head = [1,4,2,6], root = |
| 43 | + * [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] |
| 44 | + * Output: true |
| 45 | + * |
| 46 | + * |
| 47 | + * Example 3: |
| 48 | + * |
| 49 | + * |
| 50 | + * Input: head = [1,4,2,6,8], root = |
| 51 | + * [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] |
| 52 | + * Output: false |
| 53 | + * Explanation: There is no path in the binary tree that contains all the |
| 54 | + * elements of the linked list from head. |
| 55 | + * |
| 56 | + * |
| 57 | + * |
| 58 | + * Constraints: |
| 59 | + * |
| 60 | + * |
| 61 | + * 1 <= node.val <= 100 for each node in the linked list and binary tree. |
| 62 | + * The given linked list will contain between 1 and 100 nodes. |
| 63 | + * The given binary tree will contain between 1 and 2500 nodes. |
| 64 | + * |
| 65 | + */ |
| 66 | + |
| 67 | +// @lc code=start |
| 68 | +/** |
| 69 | + * Definition for singly-linked list. |
| 70 | + * struct ListNode { |
| 71 | + * int val; |
| 72 | + * ListNode *next; |
| 73 | + * ListNode(int x) : val(x), next(NULL) {} |
| 74 | + * }; |
| 75 | + */ |
| 76 | +/** |
| 77 | + * Definition for a binary tree node. |
| 78 | + * struct TreeNode { |
| 79 | + * int val; |
| 80 | + * TreeNode *left; |
| 81 | + * TreeNode *right; |
| 82 | + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} |
| 83 | + * }; |
| 84 | + */ |
| 85 | +class Solution { |
| 86 | +public: |
| 87 | + bool found; |
| 88 | + vector<int> vec; |
| 89 | + int n; |
| 90 | + long long M, C, val; |
| 91 | + void callme(long long cur, TreeNode* root){ |
| 92 | + |
| 93 | + if(root == NULL || found) |
| 94 | + return; |
| 95 | + |
| 96 | + if((int)vec.size()-n >= 0){ |
| 97 | + cur = (cur-C*(long long)vec[vec.size()-n])%M; |
| 98 | + cur = (cur+M)%M; |
| 99 | + } |
| 100 | + |
| 101 | + cur = (cur*10ll + (long long)root->val)%M; |
| 102 | + |
| 103 | + vec.push_back(root->val); |
| 104 | + if(cur == val && vec.size() >= n) |
| 105 | + found = true; |
| 106 | + |
| 107 | + callme(cur, root->left); |
| 108 | + callme(cur, root->right); |
| 109 | + vec.pop_back(); |
| 110 | + } |
| 111 | + |
| 112 | + long long poww(int k){ |
| 113 | + if(k == 0) return 1ll; |
| 114 | + if(k == 1) return 10ll; |
| 115 | + long long tmp; |
| 116 | + if( (k&1) == 0){ |
| 117 | + tmp = poww(k>>1); |
| 118 | + return (tmp*tmp)%M; |
| 119 | + } |
| 120 | + |
| 121 | + tmp = poww((k-1)>>1); |
| 122 | + tmp = (tmp*tmp)%M; |
| 123 | + return (tmp*10ll)%M; |
| 124 | + } |
| 125 | + |
| 126 | + bool isSubPath(ListNode* head, TreeNode* root) { |
| 127 | + val = 0ll; |
| 128 | + M = 1000000007ll; |
| 129 | + n = 0; |
| 130 | + while(head != NULL){ |
| 131 | + n++; |
| 132 | + val = (val*10ll+head->val)%M; |
| 133 | + head = head->next; |
| 134 | + } |
| 135 | + |
| 136 | + C = poww(n-1); |
| 137 | + vec.clear(); |
| 138 | + found = false; |
| 139 | + |
| 140 | + callme(0ll, root); |
| 141 | + return found; |
| 142 | + } |
| 143 | +}; |
| 144 | +// @lc code=end |
0 commit comments