Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path ' s sum equals the Given sum.
For example:
Given the below binary tree sum = 22 and,
5 / 4 8 / / / 4 / \ / 7 2 5 1
Return
[ [5,4,11,2], [5,8,4,5]]
https://leetcode.com/problems/path-sum-ii/
Find all the paths from the root to the leaves equal to sum.
Or keep up with the same ideas as the two questions.
Binary Tree paths:http://www.cnblogs.com/liok3187/p/4735368.html
Path sum:http://www.cnblogs.com/liok3187/p/4869521.html
1 /**2 * Definition for a binary tree node.3 * Function TreeNode (val) {4 * This.val = val;5 * This.left = This.right = null;6 * }7 */8 /**9 * @param {TreeNode} rootTen * @param {number} sum One * @return {number[][]} A */ - varPathsum =function(root, sum) { - varres = []; the if(Root && root.val!==undefined) { -Getpathsum (Root, [], 0); - } - returnRes; + - functiongetpathsum (node, path, value) { + varIsLeaf =true, TMP; A if(node.left) { atIsLeaf =false; -Getpathsum (Node.left, Path.concat (node.val), Value +node.val); - } - if(node.right) { -IsLeaf =false; -Getpathsum (Node.right, Path.concat (node.val), Value +node.val); in } - if(isleaf) { toTMP = value +Node.val; + if(TMP = = =sum) { - Res.push (Path.concat (Node.val)); the } * } $ }Panax Notoginseng};
[Leetcode] [JavaScript] Path Sum II