![]() |
VOOZH | about |
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.
Examples:
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 all the node's from the queue gets processed without returning false, then return true as the binary tree is a full binary tree.
Yes
Time Complexity: O(n).
Auxiliary Space: O(max), where max is the maximum number of nodes at a particular level.