Пример #1
0
        static void TestGetNodePath()
        {
            TreeNode<int> root;
            root = Utility.CreateBinarySearchTree();

            TreeNode<int> node = root.LeftNode.RightNode;
            Console.WriteLine("candidate node is " + node.Value);

            if (node != null)
            {
                Stack<int> stack = new Stack<int>();
                Boolean hasNode = Utility.GetNodePath(root, node, stack);
                if (hasNode)
                {
                    Console.WriteLine(String.Join(" ", stack.Select(n => n.ToString()).ToArray()));
                }
                else
                {
                    Console.WriteLine("Not contain the node");
                }
            }
            else
            {
                Console.WriteLine("the node is null");
            }
        }
Пример #2
0
        public List<string> FindAllSumPath(TreeNode<int> root, int sum)
        {
            List<string> results = new List<string>();

            Stack<int> stack = new Stack<int>();

            FindAllSumPathRecursive(root, results, stack, sum, 0);

            return results;
        }
Пример #3
0
        public void InOrderTraversalNonRecurse(TreeNode<int> root)
        {
            Stack<TreeNode<int>> stack = new Stack<TreeNode<int>>();

            Console.WriteLine(root);
            TreeNode<int> temp = root;

            while (temp != null)
            {
                Console.WriteLine(temp.LeftNode);
                if (temp.RightNode != null)
                {
                    stack.Push(temp.RightNode);
                }

                temp = temp.LeftNode;
            }
        }
Пример #4
0
		static void Main(string[] args)
		{
			Stack<int> stack = new Stack<int>();
		}
 public SweepstakesStackManager()
 {
     sweepStack = new Stack<Sweepstakes>();
 }
Пример #6
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();
        }
Пример #7
0
        public int LongestValidParenthese(string input)
        {
            if (input == null || input.Length == 0)
            {
                throw new NullReferenceException("input is null");
            }

            Stack<int> leftParenStack = new Stack<int>();

            int validNum = 0;

            for (int i = 0; i < input.Length; i++)
            {
                if (input[i] == '(')
                {
                    leftParenStack.Push(input[i]);
                }
                else if(input[i] == ')' && leftParenStack.Count != 0)
                {
                    leftParenStack.Pop();
                    validNum++;
                }
            }

            return validNum;
        }