Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Count Leaf Nodes In Binary Tree

Medium Acceptance 74.59% Points 30.00

You are given the root of a binary tree. Your task is to count and return the number of leaf nodes in the binary tree. A leaf node is a node with no left or right child.

Examples
Example 1

image Input: root = [1,2,3,4,5]
Output: 3
Explanation: The leaf nodes are 4, 5, and 3.

Example 2

image Input: root = [1]
Output: 1
Explanation: The root node is also a leaf node.

Example 3

image Input: root = []
Output: 0
Explanation: The tree contains no nodes.

Constraints
    • 0 = Number of nodes = 105
    • -10? = Node.data = 10?
Companies
Microsoft Cisco Wayfair
Topics
Tree Traversal Binary Tree
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions