Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Count Total Nodes in Binary Tree

Medium Acceptance 75.68% Points 30.00

You are given the root of a binary tree. Your task is to return the total number of nodes present in the binary tree.

Examples
Example 1

image Input: root = [1,2,3,4,5]
Output: 5
Explanation: The binary tree contains 5 nodes.

Example 2

image Input: root = [1]
Output: 1
Explanation: The tree contains only one node.

Example 3

image Input: root = []
Output: 0
Explanation: The tree is empty.

Constraints
    • 0 = Number of nodes = 105
    • -10? = Node.data = 10?
Companies
PhonePe Rubrik American Express
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