-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helper.cs
40 lines (32 loc) · 1.13 KB
/
Helper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Collections.Generic;
namespace FindLeavesOfBinaryTree
{
class Helper
{
public static TreeNode GenerateBinaryTree(int totalNodeCount)
{
var jumpCounterToChildNodes = 1;
var nodes = new TreeNode[totalNodeCount];
for (int i = 0; i < totalNodeCount / 2; ++i)
{
if (nodes[i] == null)
nodes[i] = new TreeNode(i + 1);
nodes[i].Left = nodes[i + jumpCounterToChildNodes] = new TreeNode(i + 1 + jumpCounterToChildNodes++);
nodes[i].Right = nodes[i + jumpCounterToChildNodes] = new TreeNode(i + 1 + jumpCounterToChildNodes);
}
return nodes[0];
}
internal static void PrintResults(List<List<int>> items)
{
Console.WriteLine("Results: ");
items.ForEach(item =>
{
Console.Write("[ ");
item.ForEach(sub => Console.Write($"{sub}, "));
Console.WriteLine("]");
});
Console.WriteLine("--------------------------------------");
}
}
}