VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-whether-binary-tree-full-binary-tree-not-iterative-approach/

⇱ Check whether a binary tree is a full binary tree or not | Iterative Approach - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check whether a binary tree is a full binary tree or not | Iterative Approach

Last Updated : 28 Jul, 2022

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:

  1. If (node->left == NULL && node->right == NULL), it is a leaf node. Discard it and start processing the next node from the queue.
  2. 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.
  3. 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.


Output
Yes

Time Complexity: O(n). 
Auxiliary Space: O(max), where max is the maximum number of nodes at a particular level. 

Comment
Article Tags:
Article Tags: