From ff445b7c003751366cdaebe76e3d059fa950f320 Mon Sep 17 00:00:00 2001 From: Alice Chen Date: Thu, 2 Oct 2014 14:47:00 -0400 Subject: [PATCH] Added answers for addional recursion problems --- app/recursion.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/app/recursion.js b/app/recursion.js index 4a374b6..171d33a 100644 --- a/app/recursion.js +++ b/app/recursion.js @@ -73,6 +73,30 @@ define(function() { temp.slice() ); } + }, + + fibonacci: function fib(n) { + if (n < 2) { + return n; + } + return fib(n - 1) + fib(n - 2); + }, + + validParentheses: function(n) { + var sets = []; + var getParen = function(left, right, current) { + if (left == 0 && right == 0) { + sets.push(current); + } + if (left > 0) { + getParen(left - 1, right + 1, current + '('); + } + if (right > 0) { + getParen(left, right - 1, current + ')'); + } + return sets; + }; + return getParen(n, 0, ''); } }; });