{"id":6174,"date":"2020-01-29T19:31:48","date_gmt":"2020-01-30T03:31:48","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6174"},"modified":"2020-01-29T19:32:22","modified_gmt":"2020-01-30T03:32:22","slug":"leetcode-235-lowest-common-ancestor-of-a-binary-search-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-235-lowest-common-ancestor-of-a-binary-search-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 235. Lowest Common Ancestor of a Binary Search Tree"},"content":{"rendered":"\n<p>Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.<\/p>\n\n\n\n<p>According to the&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Lowest_common_ancestor\" target=\"_blank\" rel=\"noreferrer noopener\">definition of LCA on Wikipedia<\/a>: \u201cThe lowest common ancestor is defined between two nodes p and q&nbsp;as the lowest node in T that has both p and q&nbsp;as descendants (where we allow&nbsp;<strong>a node to be a descendant of itself<\/strong>).\u201d<\/p>\n\n\n\n<p>Given binary search tree:&nbsp; root =&nbsp;[6,2,8,0,4,7,9,null,null,3,5]<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2018\/12\/14\/binarysearchtree_improved.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8\n<strong>Output:<\/strong> 6\n<strong>Explanation: <\/strong>The LCA of nodes <code>2<\/code> and <code>8<\/code> is <code>6<\/code>.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4\n<strong>Output:<\/strong> 2\n<strong>Explanation: <\/strong>The LCA of nodes <code>2<\/code> and <code>4<\/code> is <code>2<\/code>, since a node can be a descendant of itself according to the LCA definition.\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>All of the nodes&#8217; values will be unique.<\/li><li>p and q are different and both values will&nbsp;exist in the BST.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Recursion<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {    \n    if (p->val < root->val && q->val < root->val) \n      return lowestCommonAncestor(root->left, p, q);\n    if (p->val > root->val && q->val > root->val)\n      return lowestCommonAncestor(root->right, p, q);\n    return root;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the&nbsp;definition of LCA on&#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":[74,222,376,28],"class_list":["post-6174","post","type-post","status-publish","format-standard","hentry","category-tree","tag-bst","tag-easy","tag-on","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6174","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=6174"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6174\/revisions"}],"predecessor-version":[{"id":6176,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6174\/revisions\/6176"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}