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.
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.
- Check whether a binary tree is a full binary tree or not
- Iterative Approach to check if two Binary Trees are Isomorphic or not
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- 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
- Get level of a node in binary tree | iterative approach
- Deepest right leaf node in a binary tree | Iterative approach
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Construct Binary Tree from given Parent Array representation | Iterative Approach
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Largest value in each level of Binary Tree | Set-2 (Iterative Approach)
- Deepest left leaf node in a binary tree | iterative approach
- Check whether a given binary tree is skewed binary tree or not?
- Count full nodes in a Binary tree (Iterative and Recursive)
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Print all nodes at distance K from given node: Iterative Approach
- Smallest number in BST which is greater than or equal to N ( Iterative Approach)
- Check whether a given binary tree is perfect or not
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.