Given a binary tree containing n nodes. The problem is to check whether the given binary tree is a full binary tree or not. A full binary tree is defined as a binary tree in which all nodes have either zero or two child nodes. Conversely, there is no node in a full binary tree, which has only one child node.
Input : 1 / \ 2 3 / \ 4 5 Output : Yes Input : 1 / \ 2 3 / 4 Output :No
Approach: In the previous post a recursive solution has been discussed. In this post an iterative approach has been followed. Perform iterative level order traversal of the tree using queue. For each node encountered, follow the steps given below:
- If (node->left == NULL && node->right == NULL), it is a leaf node. Discard it and start processing the next node from the queue.
- If (node->left == NULL || node->right == NULL), then it means that only child of node is present. Return false as the binary tree is not a full binary tree.
- Else, push the left and right child’s of the node on to the queue.
If all the node’s from the queue gets processed without returning false, then return true as the binary tree is a full binary tree.
Time Complexity: O(n).
Auxiliary Space: O(max), where max is the maximum number of nodes at a particular level.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Check for Symmetric Binary Tree (Iterative Approach)
- Iterative approach to check if a Binary Tree is Perfect
- Iterative approach to check for children sum property in a Binary Tree
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Largest value in each level of Binary Tree | Set-2 (Iterative Approach)
- Get level of a node in binary tree | iterative approach
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Deepest right leaf node in a binary tree | Iterative approach
- Construct Binary Tree from given Parent Array representation | Iterative Approach
- Deepest left leaf node in a binary tree | iterative approach
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Count full nodes in a Binary tree (Iterative and Recursive)
- Check whether a binary tree is a full binary tree or not
- Check if a Binary Tree is BST : Simple and Efficient Approach
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
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.