Exemplo n.º 1
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;
            }
        }
Exemplo n.º 2
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();
        }
Exemplo n.º 3
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;
        }