Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
https://leetcode.com/problems/generate-parentheses/
只有两个节点的bfs
1 /** 2 * @param {number} n 3 * @return {string[]} 4 */ 5 var generateParenthesis = function(n) { 6 var res = []; 7 bfs("", 0, 0); 8 return res; 9 10 function bfs(tmpStr, countL, countR){ 11 if(countL === n && countR === n){ 12 res.push(tmpStr); 13 return; 14 } 15 if(countL !== n ){ 16 bfs(tmpStr + "(", countL + 1, countR); 17 } 18 if(countL > countR){ 19 bfs(tmpStr + ")", countL, countR + 1); 20 } 21 } 22 };