GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

How to find if a tree is an almost complete binary tree?

Let  h be the height of  the tree.

For a complete  binary tree, following conditions holds true

a) tree is a complete binary tree till level h-1.

b) nodes at level h are continous towards the left. If you do a level order traversal last level do not contain any null node in between the nodes

For example following is almost complete binary tree
     / \
     2   3
    /\   / \
   4   5 6 NULL  (note there  are no null  nodes  between 4,5,6)

Below is not an almost  complete binary tree
     / \
     2          3
    /\           /\
   4  NULL 6 NULL ( there is a null node between 4 and 6)

asked May 7, 2016 by Surendran

1 Answer

Use BFS algorithm, if you encounter NULL and check if you get NODE (!= NULL).

if you have NODE ..False

else Treu

answered May 9, 2016 by Rakesh Sharad Navale