check parentheses using stack or recursively