@@ -0,0 +1,2380 @@
+package com.sunny.grokkingalgorithms.fasttrack.week1;%0A%0Aimport com.sunny.grokkingalgorithms.ctci.c4.util.BinaryTreeNode;%0Aimport com.sunny.grokkingalgorithms.ctci.c4.util.TreeUtils;%0A%0A/**%0A * Created by sundas on 6/21/2018.%0A */%0Apublic class CheckIfBinaryTreeIsComplete %7B%0A%0A  /*%0A  In the array representation of a binary tree, if the parent node is assigned an index of i and left child gets assigned an index of 2*i + 1 while the right child is assigned an index of 2*i + 2. If we represent the above binary tree as an array with the respective indices assigned to the different nodes of the tree above from top to down and left to right.%0A%0AHence we proceed in the following manner in order to check if the binary tree is complete binary tree.%0A%0ACalculate the number of nodes (count) in the binary tree.%0AStart recursion of the binary tree from the root node of the binary tree with index (i) being set as 0 and the number of nodes in the binary (count).%0AIf the current node under examination is NULL, then the tree is a complete binary tree. Return true.%0AIf index (i) of the current node is greater than or equal to the number of nodes in the binary tree (count) i.e. (i%3E= count), then the tree is not a complete binary. Return false.%0ARecursively check the left and right sub-trees of the binary tree for same condition. For the left sub-tree use the index as (2*i + 1) while for the right sub-tree use the index as (2*i + 2).%0A   */%0A%0A  /**%0A   *%0A   * @param root%0A   * @return%0A   */%0A  public static int countNodes(BinaryTreeNode root)%7B%0A    if(root ==  null)%7B%0A      return 0;%0A    %7D%0A    return countNodes(root.left) + countNodes(root.right)+ 1;%0A  %7D%0A%0A  /**%0A   *%0A   * @param root%0A   * @param index%0A   * @param totalNodes%0A   * @return%0A   */%0A  public static boolean isComplete(BinaryTreeNode root,%0A                                   int index,%0A                                   int totalNodes)%7B%0A    if(root ==  null)%7B%0A      return true;%0A    %7D%0A    if(index %3E totalNodes)%7B%0A      return false;%0A    %7D%0A    return isComplete(root.left,2*index+1,totalNodes)%0A        && isComplete(root.right,2*index+2,totalNodes);%0A  %7D%0A%0A  /**%0A   *%0A   * @param args%0A   */%0A  public static void main(String%5B%5D args) %7B%0A    BinaryTreeNode root = TreeUtils.createBalancedBinaryTree();%0A    TreeUtils.performBfsLevelMarker(root);%0A    int nodeCount = countNodes(root);%0A    System.out.println(isComplete(root,0,nodeCount));%0A  %7D%0A%7D%0A

Check if a binary tree is complete or not.