private static void Main(string[] args) { int nodesCount = int.Parse(Console.ReadLine()); for (int i = 1; i < nodesCount; i++) { string[] edge = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); int parentValue = int.Parse(edge[0]); Tree <int> parentNode = Tree <int> .GetTreeByNodeValue(parentValue); int childValue = int.Parse(edge[1]); Tree <int> childNode = Tree <int> .GetTreeByNodeValue(childValue); parentNode.Children.Add(childNode); childNode.Parent = parentNode; } int sumPath = int.Parse(Console.ReadLine()); int subtreeSum = int.Parse(Console.ReadLine()); var rootNode = Tree <int> .FindRootNode(); Console.WriteLine("Root node: {0}", rootNode.Value); var leafNodesValues = Tree <int> .FindLeafValues().Select(tree => tree.Value).OrderBy(value => value); Console.WriteLine("Leaf nodes: {0}", string.Join(",", leafNodesValues)); var middleNodesValues = Tree <int> .FindMiddleValues().Select(tree => tree.Value).OrderBy(value => value); Console.WriteLine("Middle nodes: {0}", string.Join(",", middleNodesValues)); var longestPath = Tree <int> .FindLongestPath(); var length = longestPath.Count; Console.WriteLine( "Longest path: {0} (length = {1})", string.Join(" -> ", longestPath.Select(tree => tree.Value)), length); ICollection <IList <Tree <int> > > pathsWithGivenSum = FindAllPathsWithGivenSum(sumPath); PrintPathsWithGivenSum(pathsWithGivenSum, sumPath); ICollection <Tree <int> > subtreesWithGivenSum = new List <Tree <int> >(); FindSubtreesWithGivenSum(subtreeSum, rootNode, subtreesWithGivenSum); PrintSubtreesWithGivenSum(subtreeSum, subtreesWithGivenSum); }
public static void Main(string[] args) { int nodesCount = int.Parse(Console.ReadLine()); var nodes = new Dictionary <int, Tree>(); while (nodes.Count < nodesCount) { string input = Console.ReadLine(); var numbers = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .Select(n => int.Parse(n)) .ToArray(); var parrentNodeValue = numbers[0]; var childNodeValue = numbers[1]; CreateKeyIfNotExists(nodes, parrentNodeValue); CreateKeyIfNotExists(nodes, childNodeValue); var parrentNode = nodes[parrentNodeValue]; var childNode = nodes[childNodeValue]; parrentNode.Children.Add(childNode); childNode.Parent = parrentNode; } var pathsSum = int.Parse(Console.ReadLine()); var subtreesSum = int.Parse(Console.ReadLine()); var rootNode = nodes.Where(n => n.Value.Parent == null); if (rootNode.Count() != 1) { throw new InvalidOperationException("Invalid Tree."); } Tree resultTree = rootNode.First().Value; Console.WriteLine("Root Node: {0}", resultTree.FindRootNode()); Console.WriteLine("Leaf Nodes: {0}", string.Join(", ", resultTree.FindLeafNodes())); Console.WriteLine("Middle Nodes: {0}", string.Join(", ", resultTree.FindMiddleNodes())); Console.WriteLine("Longest Path: {0}", string.Join(" -> ", resultTree.FindLongestPath())); IList <IList <int> > pathsWithValue = resultTree.FindPathsWithSum(pathsSum); Console.WriteLine("Paths With Value {0}:", pathsSum); foreach (var path in pathsWithValue) { Console.WriteLine(string.Join(" -> ", path)); } IList <Tree> subtreesWithValue = resultTree.FindSubtreesWithSum(subtreesSum); Console.WriteLine("Subtrees With Value {0}:", subtreesSum); foreach (var tree in subtreesWithValue) { Console.WriteLine(tree); } }