using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
/*
Please print the menu tree on Console.
Each nested level should be shifted on 4 stars '*' from the parent.
The order of same level items should be the same as the order in the original list.
<c>-1</c> indicates the root level. At lease one root level will always present.
Please see the sample output:
Printed menu:
Menu1
Menu2
****Menu4
********Menu5
Menu3
****Menu8
********Menu6
********Menu7
*/
public static int level = 0;
private static readonly IList<Menu> MenuItems = new List<Menu>
new Menu(1, -1, "Menu1"),
new Menu(2, -1, "Menu2"),
new Menu(3, -1, "Menu3"),
new Menu(4, 2, "Menu4"),
new Menu(5, 4, "Menu5"),
new Menu(6, 8, "Menu6"),
new Menu(7, 8, "Menu7"),
new Menu(8, 3, "Menu8"),
};
class Menu
public Menu(int id, int parentId, string name)
Id = id;
ParentId = parentId;
Name = name;
}
public int Id { get; }
public int ParentId { get; }
public string Name { get; }
public static void Main()
Console.WriteLine("Printed menu:");
PrintMenu(MenuItems);
private static void PrintMenu(IEnumerable<Menu> menuItems)
foreach (var menu in menuItems)
var parentItems = menuItems.Where(x => x.Id == menu.ParentId);
var childItems = MenuItems.Where(x => x.ParentId == menu.Id);
if (parentItems.Count() == 0)
for (int i = 0; i < level; i++)
Console.Write("****");
Console.WriteLine(menu.Name);
if (childItems.Count() > 0)
if (parentItems.Count() > 0)
return;
level++;
PrintMenu(childItems);
level = 0;