{"id":6389,"date":"2020-03-01T19:24:56","date_gmt":"2020-03-02T03:24:56","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6389"},"modified":"2020-03-01T19:25:59","modified_gmt":"2020-03-02T03:25:59","slug":"leetcode-1367-linked-list-in-binary-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-1367-linked-list-in-binary-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1367. Linked List in Binary Tree"},"content":{"rendered":"\n<p>Given a binary tree&nbsp;<code>root<\/code>&nbsp;and a&nbsp;linked list with&nbsp;<code>head<\/code>&nbsp;as the first node.&nbsp;<\/p>\n\n\n\n<p>Return True if all the elements in the linked list starting from the&nbsp;<code>head<\/code>&nbsp;correspond to some&nbsp;<em>downward path<\/em>&nbsp;connected in the binary tree&nbsp;otherwise return False.<\/p>\n\n\n\n<p>In this context downward path means a path that starts at some node and goes downwards.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/02\/12\/sample_1_1720.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> Nodes in blue form a subpath in the binary Tree.  \n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/02\/12\/sample_2_1720.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head<\/code>.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= node.val&nbsp;&lt;= 100<\/code>&nbsp;for each node in the linked list and binary tree.<\/li><li>The given linked list will contain between&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>100<\/code>&nbsp;nodes.<\/li><li>The given binary tree will contain between&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>2500<\/code>&nbsp;nodes.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Recursion<\/strong><\/h2>\n\n\n\n<p>We need two recursion functions: isSubPath \/ isPath, the later one does a strict match.<\/p>\n\n\n\n<p>Time complexity: O(|L| * |T|)<br>Space complexity: O(|T|)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\nclass Solution {\npublic:\n  bool isSubPath(ListNode* head, TreeNode* root) {\n    if (!root) return false;\n    return isPath(head, root) || isSubPath(head, root->left) || isSubPath(head, root->right);\n  }\nprivate:\n  bool isPath(ListNode* head, TreeNode* root) {\n    if (!head) return true;\n    if (!root) return false;\n    if (root->val != head->val) return false;\n    return isPath(head->next, root->left) || isPath(head->next, root->right);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary tree&nbsp;root&nbsp;and a&nbsp;linked list with&nbsp;head&nbsp;as the first node.&nbsp; Return True if all the elements in the linked list starting from the&nbsp;head&nbsp;correspond to some&nbsp;downward&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[83,564,177,17,28],"class_list":["post-6389","post","type-post","status-publish","format-standard","hentry","category-tree","tag-list","tag-match","tag-medium","tag-recursion","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6389"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6389\/revisions"}],"predecessor-version":[{"id":6391,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6389\/revisions\/6391"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}