进入medium 的tree了, 这个有bug, 明天继续
Path Sum II
https://leetcode.com/problems/path-sum-ii/
要记得每次递归过后,要把上一次的pop()出来。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
* @param {TreeNode} root * @param {number} sum * @return {number[][]} */ var pathSum = function(root, sum) { var res = []; if(root === null){ return res; } var list = []; list.push(root.val); helper(root, sum- root.val, list, res); return res; };
var helper = function(root, sum, list, res){ if(root === null){ return ; } if(root.left === null && root.right === null && sum === 0){ res.push(list.slice()); return; } if(root.left !== null){ list.push(root.left.val); helper(root.left, sum - root.left.val, list, res); list.pop(); } if(root.right !== null){ list.push(root.right.val); helper(root.right, sum - root.right.val, list, res); list.pop(); } }
|
近期评论