Given n pairs of parentheses. Write a program to print out all valid configrations.
For example n=2, ()(), (()) are valid, but ))((, ())( are not.
foo(stack s, int count, int len)
{
if(count<0)
return ;
if(len==0)
{
if(count==0)
print s;
else return;
}
foo(s.push('('),count+1,len-1);
foo(s.push(')'),count-1.len-1);
}
Showing posts with label catalan number. Show all posts
Showing posts with label catalan number. Show all posts
Feb 2, 2009
Feb 1, 2009
merge n companies
Google interview questions, merge of N companies
Suppose we have N companies, and we want to eventually merge them into one big company. How many ways are there to merge?
Use catalan number, n number merge represents a binary tree with n-1 nodes and n leaves.
Suppose we have N companies, and we want to eventually merge them into one big company. How many ways are there to merge?
Use catalan number, n number merge represents a binary tree with n-1 nodes and n leaves.
Jan 8, 2009
How many different(in structure) Binary Search Trees can be formed from n distinct elements.
How many different(in structure) Binary Search Trees can be formed from n distinct elements.
(2n)!/(1+n)!*n!
http://en.wikipedia.org/wiki/Catalan_number
(2n)!/(1+n)!*n!
http://en.wikipedia.org/wiki/Catalan_number
Subscribe to:
Posts (Atom)