Count Leaves in a Binary Tree - aloalgo

Count Leaves in a Binary Tree

Medium

You are given the root of a binary tree, return the total number of leaf nodes. A leaf node is a node that has no children.

Example 1

Input
3920157
Output
3
Explanation:

Nodes 9, 15, and 7 are leaf nodes as they have no children. The total count is 3.

Example 2

Input
1
Output
1
Explanation:

The tree consists of a single node (1), which is also a leaf node. The total count is 1.

Example 3

Input
None
Output
0
Explanation:

An empty tree has no nodes, therefore no leaf nodes. The total count is 0.

Loading...
Input
3920157
Output
3

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!