void _printParenthesis(int pos, int n, int open, int close) { static char str[MAX_SIZE]; if(close == n) { printf("%s \n", str); return; } else { if(open > close) { str[pos] = '}'; _printParenthesis(pos+1, n, open, close+1); } if(open < n) { str[pos] = '{'; _printParenthesis(pos+1, n, open+1, close); } } }
void _printParenthesis(int pos, int n, int openb, int closeb) { static char str[MAX_SIZE]; if(closeb == n) { printf("%s \n", str); return; } else { if(openb > closeb) { str[pos] = ')'; _printParenthesis(pos+1, n, openb, closeb+1); } if(openb < n) { str[pos] = '('; _printParenthesis(pos+1, n, openb+1, closeb); } } }
/* Wrapper over _printParenthesis()*/ void printParenthesis(int n) { if(n > 0) _printParenthesis(0, n, 0, 0); return; }