{"id":513,"date":"2020-09-26T10:50:49","date_gmt":"2020-09-26T02:50:49","guid":{"rendered":"http:\/\/39.96.58.60\/?p=513"},"modified":"2022-10-18T16:39:03","modified_gmt":"2022-10-18T08:39:03","slug":"leetcode-235-%e4%ba%8c%e5%8f%89%e6%90%9c%e7%b4%a2%e6%a0%91%e7%9a%84%e6%9c%80%e8%bf%91%e5%85%ac%e5%85%b1%e7%a5%96%e5%85%88","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=513","title":{"rendered":"Leetcode 235. \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6700\u8fd1\u516c\u5171\u7956\u5148"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u641c\u7d22\u6811, \u627e\u5230\u8be5\u6811\u4e2d\u4e24\u4e2a\u6307\u5b9a\u8282\u70b9\u7684\u6700\u8fd1\u516c\u5171\u7956\u5148\u3002<\/p>\n<p><a href=\"https:\/\/baike.baidu.com\/item\/%E6%9C%80%E8%BF%91%E5%85%AC%E5%85%B1%E7%A5%96%E5%85%88\/8918834?fr=aladdin\" target=\"_blank\" rel=\"noopener noreferrer\">\u767e\u5ea6\u767e\u79d1<\/a>\u4e2d\u6700\u8fd1\u516c\u5171\u7956\u5148\u7684\u5b9a\u4e49\u4e3a\uff1a\u201c\u5bf9\u4e8e\u6709\u6839\u6811 T \u7684\u4e24\u4e2a\u7ed3\u70b9 p\u3001q\uff0c\u6700\u8fd1\u516c\u5171\u7956\u5148\u8868\u793a\u4e3a\u4e00\u4e2a\u7ed3\u70b9 x\uff0c\u6ee1\u8db3 x \u662f p\u3001q \u7684\u7956\u5148\u4e14 x \u7684\u6df1\u5ea6\u5c3d\u53ef\u80fd\u5927\uff08<strong>\u4e00\u4e2a\u8282\u70b9\u4e5f\u53ef\u4ee5\u662f\u5b83\u81ea\u5df1\u7684\u7956\u5148<\/strong>\uff09\u3002\u201d<\/p>\n<p>\u4f8b\u5982\uff0c\u7ed9\u5b9a\u5982\u4e0b\u4e8c\u53c9\u641c\u7d22\u6811:&nbsp; root =&nbsp;[6,2,8,0,4,7,9,null,null,3,5]<\/p>\n<p><img alt=\"\" src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2018\/12\/14\/binarysearchtree_improved.png\" style=\"height: 190px; width: 200px;\"><\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8\n\n<strong>\u8f93\u51fa:<\/strong> 6 \n\n<strong>\u89e3\u91ca: <\/strong>\u8282\u70b9 <code>2 <\/code>\u548c\u8282\u70b9 <code>8 <\/code>\u7684\u6700\u8fd1\u516c\u5171\u7956\u5148\u662f <code>6\u3002<\/code>\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4\n\n<strong>\u8f93\u51fa:<\/strong> 2\n\n<strong>\u89e3\u91ca: <\/strong>\u8282\u70b9 <code>2<\/code> \u548c\u8282\u70b9 <code>4<\/code> \u7684\u6700\u8fd1\u516c\u5171\u7956\u5148\u662f <code>2<\/code>, \u56e0\u4e3a\u6839\u636e\u5b9a\u4e49\u6700\u8fd1\u516c\u5171\u7956\u5148\u8282\u70b9\u53ef\u4ee5\u4e3a\u8282\u70b9\u672c\u8eab\u3002<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u8bf4\u660e:<\/strong><\/p>\n<ul>\n<li>\u6240\u6709\u8282\u70b9\u7684\u503c\u90fd\u662f\u552f\u4e00\u7684\u3002<\/li>\n<li>p\u3001q \u4e3a\u4e0d\u540c\u8282\u70b9\u4e14\u5747\u5b58\u5728\u4e8e\u7ed9\u5b9a\u7684\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u3002<\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Easy<\/p>\n<p>**\u6807\u7b7e**: \u6811\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# Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, x):\n#         self.val = x\n#         self.left = None\n#         self.right = None\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a80 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8699.61% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a17.7 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8686.69% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u56e0\u4e3a\u662f\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u5de6\u8282\u70b9\u503c\u5fc5\u5b9a\u5c0f\u4e8e\u53f3\u8282\u70b9\u3002\n    \u5982\u679c\u5f53\u524d\u8282\u70b9\u503c\u5904\u4e8e\u4fe9\u4e2a\u6307\u5b9a\u8282\u70b9\u4e4b\u95f4\uff0c\u5219\u6700\u8fd1\u516c\u5171\u7956\u5148\u5fc5\u5b9a\u4e3a\u5f53\u524d\u8282\u70b9\u3002\n    \u82e5\u4e0d\u662f\uff0c\u901a\u8fc7\u6bd4\u8f83\u8282\u70b9\u503c\uff0c\u5bf9\u6811\u8fdb\u884c\u66f4\u65b0\n\"\"\"\nclass Solution:\n    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -&gt; 'TreeNode':\n        l_val, r_val = p.val, q.val\n        if l_val &gt; r_val:       # \u786e\u4fdd\u6307\u5b9a\u8282\u70b9\u503c\u5927\u5c0f\u987a\u5e8f\n            l_val, r_val = r_val, l_val\n\n        while True:\n            if l_val &lt;= root.val &lt;= r_val:  # \u5f53\u524d\u8282\u70b9\u5904\u4e8e\u6307\u5b9a\u4e24\u8282\u70b9\u4e4b\u95f4\n                return root\n\n            if root.val &gt; r_val:    # \u5f53\u524d\u8282\u70b9\u5c0f\u4e8e\u6307\u5b9a\u8282\u70b9\u6700\u5c0f\u503c\uff0c\u66f4\u65b0\u5f53\u524d\u8282\u70b9\u4e3a\u8282\u70b9\u5de6\u5b50\u6811\n                root = root.left\n            if root.val &lt; l_val:    #\n                root = root.right\n\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a88 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8695.71% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a17.1 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8697.46% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u9012\u5f52\n\"\"\"\nclass Solution:\n    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -&gt; 'TreeNode':\n        if p.val &gt; q.val:\n            p, q = q, p\n        def find(root, p, q):\n            if p.val &lt;= root.val &lt;= q.val:  # \u6bd4\u8f83\u5f53\u524d\u8282\u70b9\u4e0ep,q\u7684\u503c\uff0c \u5982\u679c\u5904\u4e8ep,q\u4e4b\u95f4\uff0c\u5219\u8fd4\u56de\n                return root\n            elif root.val &lt; p.val:  # \u5982\u679c\u5f53\u524d\u8282\u70b9\u503c\u5c0f\u4e8ep,\u5219\u904d\u5386\u53f3\u5b50\u6811\n                return find(root.right, p, q)\n            else:   # \u5426\u5219\uff0c\u904d\u5386\u5de6\u5b50\u6811\n                return find(root.left, p, q)\n        return find(root, p, q)<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u641c\u7d22\u6811, \u627e\u5230\u8be5\u6811\u4e2d\u4e24\u4e2a\u6307\u5b9a\u8282\u70b9\u7684\u6700\u8fd1\u516c\u5171\u7956\u5148\u3002 \u767e\u5ea6\u767e\u79d1\u4e2d\u6700\u8fd1\u516c\u5171\u7956\u5148\u7684\u5b9a\u4e49\u4e3a\uff1a\u201c\u5bf9\u4e8e\u6709\u6839\u6811 T&#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":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/513"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=513"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/513\/revisions"}],"predecessor-version":[{"id":514,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/513\/revisions\/514"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=513"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}