Beispiel #1
0
        private void FindAllSumPathRecursive(TreeNode<int> node, List<string> results, Stack<int> stack, int sum, int currentSum)
        {
            if (node == null)
            {
                return;
            }

            if (node.LeftNode == null && node.RightNode == null)
            {
                if (currentSum + node.Value == sum)
                {
                    int[] array = new int[stack.Count];
                    stack.CopyTo(array, 0);
                    results.Add(node.Value + " " + string.Join(" ", array));
                }

                return;
            }

            stack.Push(node.Value);

            FindAllSumPathRecursive(node.LeftNode, results, stack, sum, currentSum + node.Value);

            FindAllSumPathRecursive(node.RightNode, results, stack, sum, currentSum + node.Value);

            stack.Pop();
        }