Size of a tree is the number of elements present in the tree. Size of the below tree is 5.
The idea is to use Level Order Traversing
1) Create an empty queue q 2) temp_node = root /*start from root*/ 3) Loop while temp_node is not NULL a) Enqueue temp_node’s children (first left then right children) to q b) Increase count with every enqueuing. c) Dequeue a node from q and assign it’s value to temp_node
Size of the tree is 5
Time Complexity: O(n)
Auxiliary Space : O(level_max) where level max is maximum number of node in any level.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Write a program to Calculate Size of a tree | Recursion
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Iterative program to count leaf nodes in a Binary Tree
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Iterative Method to find Height of Binary Tree
- Count half nodes in a Binary tree (Iterative and Recursive)
- Iterative Search for a key 'x' in Binary Tree
- Count full nodes in a Binary tree (Iterative and Recursive)
- Check for Symmetric Binary Tree (Iterative Approach)
- Iterative method to find ancestors of a given binary tree
- Get level of a node in binary tree | iterative approach
- Iterative diagonal traversal of binary tree
- Deepest left leaf node in a binary tree | iterative approach
- Deepest right leaf node in a binary tree | Iterative approach
- Largest value in each level of Binary Tree | Set-2 (Iterative Approach)
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Iterative Segment Tree (Range Maximum Query with Node Update)
- Iterative Segment Tree (Range Minimum Query)
- Iterative Preorder Traversal of an N-ary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.