Example #1
0
 public void AddChild(Node child)
 {
     child.hasParent = true;
     this.children.Add(child);
 }
Example #2
0
        static void DFS(Node node, int currSum)
        {
            currSum += node.Value;
            used.Add(node);

            for (int i = 0; i < node.ChildrenCount; i++)
            {
                if (used.Contains(node.GetNode(i)))
                {
                    continue;
                }
                DFS(node.GetNode(i), currSum);
            }
            Console.Write("{0} ->", node.Value);
            // if reached another leaf, check sums
            if (node.ChildrenCount == 1)
            {
                if (currSum > maxSum)
                {
                    maxSum = currSum;
                }
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            int elementsNum = int.Parse(Console.ReadLine());
            Dictionary<int, Node> nodes = new Dictionary<int, Node>();

            for (int i = 0; i < elementsNum-1; i++)
            {
                string nodeLink = Console.ReadLine();
                char[] separators = { '(', '<', '-', ')' };

                string[] nodeLinkValues = nodeLink.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                int parentValue = int.Parse(nodeLinkValues[0]);
                int childValue = int.Parse(nodeLinkValues[1]);

                Node parentNode;
                if (nodes.ContainsKey(parentValue))
                {
                    parentNode = nodes[parentValue];
                }
                else
                {
                    parentNode = new Node(parentValue);
                    nodes.Add(parentValue, parentNode);
                }

                Node childNode;
                if (nodes.ContainsKey(childValue))
                {
                    childNode = nodes[childValue];
                }
                else
                {
                    childNode = new Node(childValue);
                    nodes.Add(childValue, childNode);
                }

                parentNode.AddChild(childNode);
                childNode.AddChild(parentNode);

            }

            foreach (var node in nodes)
            {
                if (node.Value.ChildrenCount == 1)
                {

                    used.Clear();
                    DFS(node.Value, 0);

                    Console.WriteLine(maxSum);
                }
            }
        }