Ejemplo n.º 1
0
        public Tuple <int, int> Solve()
        {
            this.rootNode = ConvertToTree.Convert(Content);

            Tuple <int, int> result = this.DFS(this.rootNode);

            if (result != null)
            {
                return(new Tuple <int, int>(Math.Min(result.Item1, result.Item2), Math.Max(result.Item1, result.Item2)));
            }

            return(null);
        }
Ejemplo n.º 2
0
        public Tuple <int, int> Solve()
        {
            this.rootNode = ConvertToTree.Convert(Content);

            State leftState = new State()
            {
                Node = this.rootNode, Operation = LastOperation.Left
            };
            Stack <State> leftStack = new Stack <State>();

            leftStack.Push(leftState);
            this.GoLeft(leftStack);

            State rightState = new State()
            {
                Node = this.rootNode, Operation = LastOperation.Right
            };
            Stack <State> rightStack = new Stack <State>();

            rightStack.Push(rightState);
            this.GoRight(rightStack);

            while (leftStack.Count > 0 && rightStack.Count > 0)
            {
                leftState  = leftStack.Peek();
                rightState = rightStack.Peek();

                int value = leftState.Node.Value + rightState.Node.Value;
                if (value > this.X)
                {
                    this.Prev(rightStack);
                }
                else if (value < this.X)
                {
                    this.Next(leftStack);
                }
                else
                {
                    if (leftState.Node != rightState.Node)
                    {
                        return(new Tuple <int, int>(leftState.Node.Value, rightState.Node.Value));
                    }

                    return(null);
                }
            }

            return(null);
        }