{"id":497,"date":"2020-09-26T10:48:43","date_gmt":"2020-09-26T02:48:43","guid":{"rendered":"http:\/\/39.96.58.60\/?p=497"},"modified":"2022-10-18T16:39:03","modified_gmt":"2022-10-18T08:39:03","slug":"leetcode-1367-%e4%ba%8c%e5%8f%89%e6%a0%91%e4%b8%ad%e7%9a%84%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/www.yatenglg.cn\/blog\/?p=497","title":{"rendered":"Leetcode 1367. \u4e8c\u53c9\u6811\u4e2d\u7684\u5217\u8868"},"content":{"rendered":"<p>\u7ed9\u4f60\u4e00\u68f5\u4ee5&nbsp;<code>root<\/code>&nbsp;\u4e3a\u6839\u7684\u4e8c\u53c9\u6811\u548c\u4e00\u4e2a&nbsp;<code>head<\/code>&nbsp;\u4e3a\u7b2c\u4e00\u4e2a\u8282\u70b9\u7684\u94fe\u8868\u3002<\/p>\n<p>\u5982\u679c\u5728\u4e8c\u53c9\u6811\u4e2d\uff0c\u5b58\u5728\u4e00\u6761\u4e00\u76f4\u5411\u4e0b\u7684\u8def\u5f84\uff0c\u4e14\u6bcf\u4e2a\u70b9\u7684\u6570\u503c\u6070\u597d\u4e00\u4e00\u5bf9\u5e94\u4ee5&nbsp;<code>head<\/code>&nbsp;\u4e3a\u9996\u7684\u94fe\u8868\u4e2d\u6bcf\u4e2a\u8282\u70b9\u7684\u503c\uff0c\u90a3\u4e48\u8bf7\u4f60\u8fd4\u56de <code>True<\/code> \uff0c\u5426\u5219\u8fd4\u56de <code>False<\/code> \u3002<\/p>\n<p>\u4e00\u76f4\u5411\u4e0b\u7684\u8def\u5f84\u7684\u610f\u601d\u662f\uff1a\u4ece\u6811\u4e2d\u67d0\u4e2a\u8282\u70b9\u5f00\u59cb\uff0c\u4e00\u76f4\u8fde\u7eed\u5411\u4e0b\u7684\u8def\u5f84\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<p><strong><img alt=\"\" src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2020\/02\/29\/sample_1_1720.png\" style=\"height: 280px; width: 220px;\"><\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>true\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u6811\u4e2d\u84dd\u8272\u7684\u8282\u70b9\u6784\u6210\u4e86\u4e0e\u94fe\u8868\u5bf9\u5e94\u7684\u5b50\u8def\u5f84\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<p><strong><img alt=\"\" src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2020\/02\/29\/sample_2_1720.png\" style=\"height: 280px; width: 220px;\"><\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/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\n<strong>\u8f93\u51fa\uff1a<\/strong>true\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/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\n<strong>\u8f93\u51fa\uff1a<\/strong>false\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u4e8c\u53c9\u6811\u4e2d\u4e0d\u5b58\u5728\u4e00\u4e00\u5bf9\u5e94\u94fe\u8868\u7684\u8def\u5f84\u3002\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li>\u4e8c\u53c9\u6811\u548c\u94fe\u8868\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u7684\u503c\u90fd\u6ee1\u8db3&nbsp;<code>1 &lt;= node.val&nbsp;&lt;= 100<\/code>&nbsp;\u3002<\/li>\n<li>\u94fe\u8868\u5305\u542b\u7684\u8282\u70b9\u6570\u76ee\u5728&nbsp;<code>1<\/code>&nbsp;\u5230&nbsp;<code>100<\/code>&nbsp;\u4e4b\u95f4\u3002<\/li>\n<li>\u4e8c\u53c9\u6811\u5305\u542b\u7684\u8282\u70b9\u6570\u76ee\u5728&nbsp;<code>1<\/code>&nbsp;\u5230&nbsp;<code>2500<\/code>&nbsp;\u4e4b\u95f4\u3002<\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6811\u3001 \u94fe\u8868\u3001 \u52a8\u6001\u89c4\u5212\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a108 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8696.06% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a15.8 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8625.37% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u5148\u5728\u6811\u4e2d\u627e\u5230\u4e0e\u94fe\u8868\u7b2c\u4e00\u4e2a\u8282\u70b9\u76f8\u540c\u503c\u7684 \u6811\u8282\u70b9\n    \u7136\u540e\u4ee5\u8fd9\u4e9b\u6811\u4e2d\u8282\u70b9\u4e3a\u6839\u7684\u5b50\u6811\u4e2d\u5bfb\u627e\u662f\u5426\u5b58\u5728\u94fe\u8868\n\"\"\"\nclass Solution:\n    def isSubPath(self, head: ListNode, root: TreeNode) -&gt; bool:\n\n        def Sub(root, head):    # \u9012\u5f52\u5728\u5b50\u6811\u4e2d\u5224\u65ad\u662f\u5426\u5f53\u524d\u5b50\u6811\u5b58\u5728\u94fe\u8868\n            # print(root, head)\n            if head==None:  # \u5982\u679c\u94fe\u8868\u5339\u914d\u5b8c\uff0c\u5219\u8fd4\u56deTrue\uff0c\u5b58\u5728\n                return True\n\n            elif root and head:\n                if root.val == head.val:\n                    if Sub(root.left, head.next):\n                        return True\n                    if Sub(root.right, head.next):\n                        return True\n\n        nodes = []\n        def find(root, head):   # \u9012\u5f52\u5bfb\u627e\u6811\u4e2d\u6240\u6709\u4e0e\u94fe\u8868\u9996\u8282\u70b9\u503c\u76f8\u540c\u7684\u6811\u8282\u70b9\n            if root:\n                if root.val == head.val:    # \u5982\u679c\u5f53\u524d\u8282\u70b9\u503c\u7b49\u4e8e\u94fe\u8868\u9996\u8282\u70b9\u503c\uff0c\u8bb0\u5f55\n                    nodes.append(root)\n                find(root.left, head)\n                find(root.right, head)\n        find(root, head)\n\n        for node in nodes:\n            if Sub(node, head): # \u5224\u65ad\u6240\u6709\u5b50\u6811\u662f\u5426\u5b58\u5728\n                return True\n        return False<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u68f5\u4ee5&nbsp;root&nbsp;\u4e3a\u6839\u7684\u4e8c\u53c9\u6811\u548c\u4e00\u4e2a&nbsp;head&nbsp;\u4e3a\u7b2c\u4e00\u4e2a\u8282\u70b9\u7684\u94fe\u8868&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/497"}],"collection":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=497"}],"version-history":[{"count":1,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/497\/revisions"}],"predecessor-version":[{"id":498,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/497\/revisions\/498"}],"wp:attachment":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}