↓快轉到主要內容
  1. LeetCode/

LeetCode 222: Count Complete Tree Nodes

·1 分鐘· ·
LeetCode Medium Binary-Search Bit-Manipulation Tree Binary-Tree
Wei Yi Chung
作者
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
目錄

基本資料
#

難易度: medium 第一次嘗試:2022-11-18

  • 提交紀錄:Runtime Error → Accepted ×3 → Restrictions Failed → Accepted

解題思路
#

完全二元樹的性質:比較右子樹的最左深度和左子樹的最左深度(Tree):

  • 一樣深:左子樹是滿的,節點數是 2^h - 1,加上根,再遞迴算右子樹。
  • 右邊比較淺:右子樹是滿的(高度少一),再遞迴算左子樹。

每層只往一邊遞迴,每次算高度 O(log n),總共 O(log² n)。

我的嘗試
#

2022 年第一版變數沒定義,之後用一般的遍歷數節點(O(n))就過了。2026 年重寫時改成比較最左和最右的高度:一樣高代表整棵樹是滿的,直接用公式,不然就遞迴左右兩邊,也是 O(log² n)。這裡的寫法改成比較左右子樹的最左深度,每層只遞迴一邊。

正確解法
#

class Solution:
    def countNodes(self, root: Optional[TreeNode]) -> int:
        def depth(node):
            d = 0
            while node:
                d, node = d + 1, node.left
            return d
        if not root:
            return 0
        l, r = depth(root.left), depth(root.right)
        if l == r:                                   # 左子樹是滿的
            return (1 << l) + self.countNodes(root.right)
        return (1 << r) + self.countNodes(root.left)    # 右子樹是滿的

複雜度
#

時間 O(log² n),空間 O(log n)。

要特別避免的錯誤
#

  • 1 << h 已經包含了根:2^h - 1 個子樹節點加上 1 個根。

面試口說整理
#

完全二元樹的左右子樹一定有一邊是滿的。我比較兩邊的最左深度,滿的那邊直接用公式算,另一邊遞迴。每層只走一邊,時間 O(log² n)。

相關文章

LeetCode 113: Path Sum II
·1 分鐘
LeetCode Medium Backtracking Tree Depth-First-Search Binary-Tree
LeetCode 113 解題筆記:回溯記錄目前的路徑,到葉子時總和等於目標就存一份複本;值可能是負的,不能提早剪枝
LeetCode 129: Sum Root to Leaf Numbers
·1 分鐘
LeetCode Medium Tree Depth-First-Search Binary-Tree
LeetCode 129 解題筆記:往下走時目前的數字乘 10 加上節點的值,到葉子時加進總和
LeetCode 236: Lowest Common Ancestor of a Binary Tree
·1 分鐘
LeetCode Medium Tree Depth-First-Search Binary-Tree Binary-Lifting
LeetCode 236 解題筆記:遞迴回傳子樹裡找到的 p 或 q;左右都找到代表在這裡分岔,這個節點就是 LCA
LeetCode 337: House Robber III
·1 分鐘
LeetCode Medium Dynamic-Programming Tree Depth-First-Search Binary-Tree
LeetCode 337 解題筆記:樹上 DP,每個節點回傳(搶它、不搶它)的最大值;不是一層一層交錯搶
LeetCode 102: Binary Tree Level Order Traversal
·1 分鐘
LeetCode Medium Tree Breadth-First-Search Binary-Tree
LeetCode 102 解題筆記:BFS 一次處理一整層,用目前這層的節點產生下一層;空樹回傳空 list
LeetCode 1448: Count Good Nodes in Binary Tree
·1 分鐘
LeetCode Medium Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 1448 解題筆記:DFS 往下傳路徑上的最大值,節點的值大於等於它就是好節點