public static ArrayList<LinkedList<TreeNode>> level(TreeNode root) {
		if (root == null) {
			return null;
		}
		int level = 0;
		ArrayList<LinkedList<TreeNode>> result = new ArrayList<LinkedList<TreeNode>>();
		LinkedList<TreeNode> levelList = new LinkedList<TreeNode>();
		// Added root to the level list so that it can be added to the main
		// result list later
		levelList.add(root);
		// Adding root to the result list
		result.add(level, levelList);
		while (true) {
			// Creating a new list for each level
			levelList = new LinkedList<TreeNode>();
			/*
			 * Getting into result list and then into the specific index(level)
			 * and extracting the TreeNodes from the levelList and checking if
			 * the nodes in levelList have any children and add them to
			 * levelList if exists
			 */
			for (int i = 0; i < result.get(level).size(); i++) {
				TreeNode n = result.get(level).get(i);
				if (n.left != null) {
					levelList.add(n.left);
				}
				if (n.right != null) {
					levelList.add(n.right);
				}
			}
			// If the list is not empty then add it to result
			if (levelList.size() > 0) {
				result.add(level + 1, levelList);
				// List empty so don't add to result and we have reached at the
				// leaf nodes of the tree
			} else {
				break;
			}
			// Increment level to go to next level and repeat the procedure
			level++;
		}
		return result;
	}