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.
- Write a program to Calculate Size of a tree | Recursion
- Iterative program to count leaf nodes in a Binary Tree
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Iterative Preorder Traversal of an N-ary Tree
- Iterative Search for a key 'x' in Binary Tree
- Iterative diagonal traversal of binary tree
- Iterative Boundary traversal of Complete Binary tree
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Iterative method to find ancestors of a given binary tree
- Iterative Segment Tree (Range Minimum Query)
- Check for Symmetric Binary Tree (Iterative Approach)
- Get level of a node in binary tree | iterative approach
- Largest value in each level of Binary Tree | Set-2 (Iterative Approach)
- Iterative approach to check if a Binary Tree is Perfect
- Iterative Method to find Height of Binary 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 email@example.com. 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.
Improved By : 29AjayKumar