Leetcode:path Sum III

Source: Internet
Author: User

You are given a binary treeinchwhich each node contains an integer value. Find the number of paths this sum to a given value. The path does not need to start or end on the root or a leaf, but it must go downwards (traveling only fromparent nodes to child nodes). The tree has no more than1, theNodes and the values areinchThe range-1, the, theTo1, the, the. Example:root= [Ten,5,-3,3,2,NULL, One,3,-2,NULL,1], sum =8      Ten/      5-3/ \      3   2    One/ \   3-2   1Return3. The paths, sum to8is :1.5-32.5-2-13. -3- One

Add the prefix sum to the HASHMAP, and check along path if Hashmap.contains (Pathsum+cur.val-target);

My Solution

1 /**2 * Definition for a binary tree node.3 * public class TreeNode {4 * int val;5 * TreeNode left;6 * TreeNode right;7 * TreeNode (int x) {val = x;}8  * }9  */Ten  Public classSolution { One      Public intPathsum (TreeNode root,intsum) { A         if(Root = =NULL)return0; -arraylist<integer> res =NewArraylist<integer>(); -Res.add (0); theHashmap<integer, integer> map =NewHashmap<integer, integer>(); -Map.put (0, 1); -Helper (root, sum, 0, res, map); -         returnRes.get (0); +     } -      +      Public voidHelper (TreeNode cur,intTargetintPathsum, arraylist<integer> Res, Hashmap<integer, integer>map) { A         if(Map.containskey (pathsum+cur.val-target)) { atRes.set (0, res.get (0) + map.get (pathsum+cur.val-target)); -         } -         if(!map.containskey (pathsum+cur.val)) { -Map.put (Pathsum+cur.val, 1); -         } -         ElseMap.put (Pathsum+cur.val, Map.get (pathsum+cur.val) +1); in         if(Cur.left! =NULL) helper (Cur.left, Target, pathsum+Cur.val, res, map); -         if(Cur.right! =NULL) helper (cur.right, Target, pathsum+Cur.val, res, map); toMap.put (Pathsum+cur.val, Map.get (pathsum+cur.val)-1); +     } -}

A more concise solution:

1      Public intPathsum (TreeNode root,intsum) {2Hashmap<integer, integer> presum =NewHashMap ();3Presum.put (0,1);4         returnHelper (root, 0, Sum, presum);5     }6     7      Public intHelper (TreeNode root,intSumintTarget, Hashmap<integer, integer>presum) {8         if(Root = =NULL) {9             return0;Ten         } One          ASum + =Root.val; -         intres = Presum.getordefault (sum-target, 0); -Presum.put (sum, presum.getordefault (sum, 0) + 1); the          -Res + = Helper (root.left, sum, Target, presum) +Helper (root.right, sum, Target, presum); -Presum.put (sum, presum.get (sum)-1); -         returnRes; +}

Leetcode:path Sum III

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.