<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      2025/09/14 【二叉樹11】完全二叉樹的節點個數

      222. 完全二叉樹的節點個數 - 力扣(LeetCode)

      1.迭代法,層序遍歷

      class Solution:
          def countNodes(self, root: Optional[TreeNode]) -> int:
              count = 0
              if not root:
                  return count
              queue = collections.deque([root])
              while queue:
                  for _ in range(len(queue)):
                      node = queue.popleft()
                      count += 1
                      if node.left:
                          queue.append(node.left)
                      if node.right:
                          queue.append(node.right)
              return count

       

      2.遞歸法

      解法(1)后序遍歷遞歸

      class Solution:
          def countNodes(self, root: Optional[TreeNode]) -> int:
              if not root:
                  return 0
              leftNodes = self.countNodes(root.left)
              rightNodes = self.countNodes(root.right)
              return 1 + leftNodes + rightNodes

      精簡寫法:

      class Solution:
          def countNodes(self, root: Optional[TreeNode]) -> int:
              if not root:
                  return 0
              return 1 + self.countNodes(root.left) + self.countNodes(root.right)

      解法(2)前序遍歷遞歸

      class Solution:
          def countNodes(self, root: Optional[TreeNode]) -> int:
              if not root:
                  return 0
      
              self.count = 0
              self.getCount(root)
              return self.count
          
          def getCount(self, node):
              if not node:
                  return
              self.count += 1
              self.getCount(node.left)
              self.getCount(node.right)

       

      3.根據完全二叉樹特性寫的遞歸法

      寫法1:

      class Solution:
          def countNodes(self, root: Optional[TreeNode]) -> int:
              if not root:
                  return 0
              left = root.left
              right = root.right
              leftDepth = 0
              rightDepth = 0
      
              while left:
                  leftDepth += 1
                  left = left.left
      
              while right:
                  rightDepth += 1
                  right = right.right
      
              if leftDepth == rightDepth:
                  return (2<<leftDepth) - 1
      
              return self.countNodes(root.left) + self.countNodes(root.right) + 1

      寫法1.1:

      class Solution:
          def countNodes(self, root: Optional[TreeNode]) -> int:
              if not root:
                  return 0
              left = root.left
              right = root.right
              leftDepth = 1
              rightDepth = 1
      
              while left:
                  leftDepth += 1
                  left = left.left
      
              while right:
                  rightDepth += 1
                  right = right.right
      
              if leftDepth == rightDepth:
                  return 2**leftDepth - 1
      
              return self.countNodes(root.left) + self.countNodes(root.right) + 1

      知識點:

      ①. 以下的兩種寫法不同

      寫法1:return (2 << leftDepth) - 1
      寫法2:return 2 ** leftDepth - 1

      它們的結果不一樣,因為 << 是移位運算符,而 ** 是指數(冪)運算。

      解釋1:

      • 2 << leftDepth把數字 2 左移 leftDepth

      • 位運算規律是:x << n == x * (2^n)

      • 所以 2 << leftDepth == 2 * (2^leftDepth) == 2^(leftDepth + 1)

      (2 << n) - 1 == 2 ** (n + 1) - 1

      解釋2:

      • 2 ** leftDepth 是指數運算,直接表示 2^leftDepth

      進一步的:

      (1 << n) == 2 ** n

      因為 1 << n 表示二進制數 1 向左移動 n 位,也就是 2^n
      2 << n == 2 * 2^n == 2^(n+1)

      ②錯誤寫法

      return 2 << leftDepth - 1

      問題出在運算優先級

      在 Python 中,-(減法)優先級高于 <<(左移)。

      所以這行代碼等價于:

      return 2 << (leftDepth - 1)

      寫法2:

      class Solution:
          def countNodes(self, root: Optional[TreeNode]) -> int:
              if not root: return 0
              count = 1
              left = root.left; right = root.right
              while left and right:
                  count += 1
                  left = left.left; right = right.right
              if not left and not right:
                  return 2**count - 1
              return 1 + self.countNodes(root.left) + self.countNodes(root.right)

      寫法2.2:

      class Solution:
          def countNodes(self, root: Optional[TreeNode]) -> int:
              if not root: return 0
              count = 0
              left = root.left; right = root.right
              while left and right:
                  count += 1
                  left = left.left; right = right.right
              if not left and not right:
                  return (2<<count) - 1
              return 1 + self.countNodes(root.left) + self.countNodes(root.right)

       

      posted on 2025-09-14 17:25  axuu  閱讀(8)  評論(0)    收藏  舉報

      主站蜘蛛池模板: 国产精品剧情亚洲二区| 国内精品极品久久免费看| 人妻少妇精品视频专区| 欧美成人精品三级网站| 欧美色欧美亚洲高清在线视频| 国产三级精品三级在线区| 国产精品成人中文字幕| 国产精品无码dvd在线观看| 成人av天堂网在线观看| 好看的国产精品自拍视频| 久久精品国产99久久6| 四虎永久免费高清视频| 大又大又粗又硬又爽少妇毛片| 免费人成网站视频在线观看| 诏安县| 激情综合色综合啪啪开心| 久久精品国产九一九九九| 国产又色又爽又黄的网站免费| 久久精品国产99国产精品严洲 | 国产免费爽爽视频| 乱色老熟妇一区二区三区| 精品久久丝袜熟女一二三| 欧美牲交a欧美牲交aⅴ一| 中文字幕有码日韩精品| 插入中文字幕在线一区二区三区| 深夜精品免费在线观看| 少妇高潮毛片免费看| 国产精品九九九一区二区| 精品人妻二区中文字幕| 成人午夜av在线播放| 亚洲日本一区二区三区在线播放| 亚洲精品成人福利网站| 亚洲国产美女精品久久久| 日韩深夜福利视频在线观看| 中文人妻无码一区二区三区在线| 亚洲精品亚洲人成人网| 久久精品国产熟女亚洲av| 亚洲国产一区二区三区| 成人精品日韩专区在线观看| 国产超碰无码最新上传| 丁香五香天堂网|