{"id":955,"date":"2025-12-14T01:48:11","date_gmt":"2025-12-13T17:48:11","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=955"},"modified":"2025-12-14T01:48:11","modified_gmt":"2025-12-13T17:48:11","slug":"leetcode-236-%e4%ba%8c%e5%8f%89%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":"https:\/\/www.guanhaobo.cn\/?p=955","title":{"rendered":"LeetCode 236 &#8211; \u4e8c\u53c9\u6811\u7684\u6700\u8fd1\u516c\u5171\u7956\u5148"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811, \u627e\u5230\u8be5\u6811\u4e2d\u4e24\u4e2a\u6307\u5b9a\u8282\u70b9\u7684\u6700\u8fd1\u516c\u5171\u7956\u5148\u3002<br \/>\n\u767e\u5ea6\u767e\u79d1\u4e2d\u6700\u8fd1\u516c\u5171\u7956\u5148\u7684\u5b9a\u4e49\u4e3a\uff1a\u201c\u5bf9\u4e8e\u6709\u6839\u6811 T \u7684\u4e24\u4e2a\u8282\u70b9 p\u3001q\uff0c\u6700\u8fd1\u516c\u5171\u7956\u5148\u8868\u793a\u4e3a\u4e00\u4e2a\u8282\u70b9 x\uff0c\u6ee1\u8db3 x \u662f p\u3001q \u7684\u7956\u5148\u4e14 x \u7684\u6df1\u5ea6\u5c3d\u53ef\u80fd\u5927\uff08\u4e00\u4e2a\u8282\u70b9\u4e5f\u53ef\u4ee5\u662f\u5b83\u81ea\u5df1\u7684\u7956\u5148\uff09\u3002\u201d<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>\u8bbe t1 = \u6839\u7ed3\u70b9\u662fp\u6216q\uff1bt2 = \u5de6\u5b50\u6811\u6709 p \u6216 q\uff1bt3 = \u53f3\u5b50\u6811\u6709 p \u6216 q.<br \/>\n\u5219 t1, t2, t3 \u4e2d\u4efb\u610f\u4e24\u4e2a\u540c\u65f6\u6210\u7acb\u65f6\uff0c\u5f53\u524d\u6839\u7ed3\u70b9\u662f p \u6216 q \u7684\u6700\u8fd1\u516c\u5171\u7956\u5148.<br \/>\n\u56e0\u6b64 DFS \u4e00\u4e0b\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">private TreeNode ans;\n\npublic TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {\n    dfs(root, p, q, 1);\n    return ans;\n}\n\nprivate boolean dfs(TreeNode root, TreeNode p, TreeNode q, int depth) {\n    if (root == null) {\n        return false;\n    }\n    boolean find = false;\n    if (root == p || root == q) {\n        find = true;\n    }\n    boolean left = dfs(root.left, p, q, depth + 1);\n    boolean right = dfs(root.right, p, q, depth + 1);\n    if ((left &amp;&amp; right) || (left &amp;&amp; find) || (right &amp;&amp; find)) {\n        ans = root;\n    }\n    return find || left || right;\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">private var ans: TreeNode? = null\n\nfun lowestCommonAncestor(root: TreeNode?, p: TreeNode?, q: TreeNode?): TreeNode? {\n    dfs(root, p, q, 1)\n    return ans\n}\n\nprivate fun dfs(root: TreeNode?, p: TreeNode?, q: TreeNode?, depth: Int): Boolean {\n    if (root == null) {\n        return false\n    }\n    var find = false\n    val left = dfs(root.left, p, q, depth + 1)\n    val right = dfs(root.right, p, q, depth + 1)\n    if (root == p || root == q) {\n        find = true;\n    }\n\n    if ((left &amp;&amp; right) || (find &amp;&amp; left) || (find &amp;&amp; right)) {\n        ans = root\n    }\n    return find || left || right\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[11,20,33],"class_list":["post-955","post","type-post","status-publish","format-standard","hentry","category-algo","tag-dfs","tag-leetcode","tag-33"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/955","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=955"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/955\/revisions"}],"predecessor-version":[{"id":956,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/955\/revisions\/956"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}