Tensorum
YouTubeশেখা শুরু করুন
LeetCode 222 · Other

Count Complete Tree Nodes

LeetCode 222 · মাঝারি · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু

count = 1 + count(left) + count(right).

মাঝারিBinary SearchBit ManipulationTreeBinary Tree
LeetCode-এ খুলুন ↗

আমি Count Complete Tree Nodes (LeetCode 222) এভাবে সমাধান করেছি — Other প্যাটার্নের একটি মাঝারি প্রবলেম। নিচে: intuition, ধাপে ধাপে approach, আমার accepted Python কোড, animated test cases আর line-by-line debugger।

নিচের বিবরণ ইংরেজিতে।

Intuition

Counting every node recursively is the simplest correct answer: one for the root plus the counts of both subtrees.

Approach

  1. 1

    Empty → 0; leaf → 1.

  2. 2

    Otherwise 1 + both subtree counts.

আমি কীভাবে সমাধান করেছি

Recursive countPythonO(n) time
class Solution:
    def countNodes(self, root):
        if root is None:
            return 0
        if root.left is None and root.right is None:
            return 1
        return 1 + self.countNodes(root.left) + self.countNodes(root.right)
স্টেপ-থ্রু লোড হচ্ছে…

Count Complete Tree Nodes — সাধারণ প্রশ্ন

Count Complete Tree Nodes-এর পেছনে intuition কী?

Counting every node recursively is the simplest correct answer: one for the root plus the counts of both subtrees.

Count Complete Tree Nodes (LeetCode 222) কীভাবে সমাধান করবেন?

1. Empty → 0; leaf → 1. 2. Otherwise 1 + both subtree counts.

Count Complete Tree Nodes-এর time আর space complexity কত?

Time: O(n)। Space: O(log n) for a complete tree। (Accepted approach: Recursive count।)

Count Complete Tree Nodes-এ সাধারণ ভুলগুলো কী?

Comparing left and right heights gives O(log² n) by using completeness.