Count number of nodes in a Binary Tree (Leetcode 222)

It is also called as the size of a binary tree. Technique: FAITH technique of Recursion

Problem Link 1: https://leetcode.com/problems/count-complete-tree-nodes/

Problem Link 2: https://practice.geeksforgeeks.org/problems/size-of-binary-tree/1

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

Last updated