<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)235:二叉搜索樹的最近公共祖先

          共 3904字,需瀏覽 8分鐘

           ·

          2021-04-13 15:19

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做 二叉搜索樹的最近公共祖先,我們先來看題面:
          https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/

          Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.


          According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”


          給定一個二叉搜索樹, 找到該樹中兩個指定節(jié)點的最近公共祖先。


          示例


          示例 1:
          輸入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
          輸出: 6 
          解釋: 節(jié)點 2 和節(jié)點 8 的最近公共祖先是 6。

          示例 2:
          輸入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
          輸出: 2
          解釋: 節(jié)點 2 和節(jié)點 4 的最近公共祖先是 2, 因為根據(jù)定義最近公共祖先節(jié)點可以為節(jié)點本身。

          說明:
          所有節(jié)點的值都是唯一的。
          p、q 為不同節(jié)點且均存在于給定的二叉搜索樹中。



          解題


          這題讓求二叉搜索樹的最近公共祖先,而二叉搜索樹的特點就是左子樹的所有節(jié)點都小于當(dāng)前節(jié)點,右子樹的所有節(jié)點都大于當(dāng)前節(jié)點,并且每棵子樹都具有上述特點,所以這題就好辦了,從更節(jié)點開始遍歷

          • 如果兩個節(jié)點值都小于根節(jié)點,說明他們都在根節(jié)點的左子樹上,我們往左子樹上找

          • 如果兩個節(jié)點值都大于根節(jié)點,說明他們都在根節(jié)點的右子樹上,我們往右子樹上找

          • 如果一個節(jié)點值大于根節(jié)點,一個節(jié)點值小于根節(jié)點,說明他們他們一個在根節(jié)點的左子樹上一個在根節(jié)點的右子樹上,那么根節(jié)點就是他們的最近公共祖先節(jié)點。


          遞歸法


          class Solution(object):
              def lowestCommonAncestor(self, root, p, q):
                  """
                  :type root: TreeNode
                  :type p: TreeNode
                  :type q: TreeNode
                  :rtype: TreeNode
                  """

                  if root.val < p.val and root.val < q.val:
                      return self.lowestCommonAncestor(root.right, p, q)
                  elif root.val > p.val and root.val > q.val:
                      return self.lowestCommonAncestor(root.left, p, q)
                  else:
                      return root


          非遞歸:

          class Solution(object):
              def lowestCommonAncestor(self, root, p, q):
                  """
                  :type root: TreeNode
                  :type p: TreeNode
                  :type q: TreeNode
                  :rtype: TreeNode
                  """

                  while root:
                      if root.val > max(p.val, q.val):
                          root = root.left
                      elif root.val < min(p.val, q.val):
                          root = root.right
                      else:
                          return root
                  return root



          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力 。

          上期推文:

          LeetCode1-220題匯總,希望對你有點幫助!

          LeetCode刷題實戰(zhàn)221:最大正方形

          LeetCode刷題實戰(zhàn)222:完全二叉樹的節(jié)點個數(shù)

          LeetCode刷題實戰(zhàn)223:矩形面積

          LeetCode刷題實戰(zhàn)224:基本計算器

          LeetCode刷題實戰(zhàn)225:用隊列實現(xiàn)棧

          LeetCode刷題實戰(zhàn)226:翻轉(zhuǎn)二叉樹

          LeetCode刷題實戰(zhàn)227:基本計算器 II

          LeetCode刷題實戰(zhàn)228:匯總區(qū)間

          LeetCode刷題實戰(zhàn)229:求眾數(shù) II

          LeetCode刷題實戰(zhàn)230:二叉搜索樹中第K小的元素

          LeetCode刷題實戰(zhàn)231:2的冪

          LeetCode刷題實戰(zhàn)232:用棧實現(xiàn)隊列

          LeetCode刷題實戰(zhàn)233:數(shù)字 1 的個數(shù)

          LeetCode刷題實戰(zhàn)234:回文鏈表


          瀏覽 60
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  精品国无人区一品二品三品 | 河南首富 越来越富 | 国产日韩精品无码 | 99久久99久久精品免费看蜜桃 | 色五月综合网 |