# Category Archives: Stack

## Iterative Postorder Traversal | Set 2 (Using One Stack)

We have discussed a simple iterative postorder traversal using two stacks in the previous post. In this post, an approach with only one stack is… Read More »

## Iterative Postorder Traversal | Set 1 (Using Two Stacks)

We have discussed iterative inorder and iterative preorder traversals. In this post, iterative postorder traversal is discussed, which is more complex than the other two… Read More »

## The Stock Span Problem

The stock span problem is a financial problem where we have a series of n daily price quotes for a stock and we need to… Read More »

## Implement Stack using Queues

The problem is opposite of this post. We are given a Queue data structure that supports standard operations like enqueue() and dequeue(). We need to… Read More »

## Construct BST from given preorder traversal | Set 2

Given preorder traversal of a binary search tree, construct the BST. For example, if the given traversal is {10, 5, 1, 7, 40, 50}, then… Read More »

## The Celebrity Problem

In a party of N people, only one person is known to everyone. Such a person may be present in the party, if yes, (s)he… Read More »

## Implement two stacks in an array

Create a data structure twoStacks that represents two stacks. Implementation of twoStacks should use only one array, i.e., both stacks should use the same array… Read More »

## Design and Implement Special Stack Data Structure | Added Space Optimized Version

Question: Design a Data Structure SpecialStack that supports all the stack operations like push(), pop(), isEmpty(), isFull() and an additional operation getMin() which should return… Read More »

## Spaghetti Stack

Spaghetti stack A spaghetti stack is an N-ary tree data structure in which child nodes have pointers to the parent nodes (but not vice-versa) Spaghetti… Read More »

## Next Greater Element

Given an array, print the Next Greater Element (NGE) for every element. The Next greater Element for an element x is the first greater element… Read More »

## Reverse a stack using recursion

Write a program to reverse a stack using recursion. You are not allowed to use loop constructs like while, for..etc, and you can only use… Read More »

## Check for balanced parentheses in an expression

Given an expression string exp , write a program to examine whether the pairs and the orders of “{“,”}”,”(“,”)”,”[“,”]” are correct in exp. Example: Input:… Read More »

## Inorder Tree Traversal without Recursion

Using Stack is the obvious way to traverse tree without recursion. Below is an algorithm for traversing binary tree using stack. See this for step… Read More »

## Queue using Stacks

The problem is opposite of this post. We are given a stack data structure with push and pop operations, the task is to implement a… Read More »

## Level order traversal in spiral form

Write a function to print spiral order traversal of a tree. For below tree, function should print 1, 2, 3, 4, 5, 6, 7. Recommended:… Read More »