LeetCode Path Sum III

本文解析了LeetCode上路径总和III的问题,提供了两种解决方案:一种是使用深度优先搜索(DFS)的方法,时间复杂度为O(n²);另一种是通过维护前缀和并利用哈希表进行查找的方法,时间复杂度降低到了O(n)。

原题链接在这里:https://leetcode.com/problems/path-sum-iii/

题目:

You are given a binary tree in which each node contains an integer value.

Find the number of paths that sum to a given value.

The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).

The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.

Example:

root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8

      10
     /  \
    5   -3
   / \    \
  3   2   11
 / \   \
3  -2   1

Return 3. The paths that sum to 8 are:

1.  5 -> 3
2.  5 -> 2 -> 1
3. -3 -> 11

题解:

Path Sum II类似. 也可以采用DFS, 从每一个点都需要开始一次新的DFS.

Time Complexity: O(n^2). Space: O(logn), stack space.

AC Java:

 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  */
10 public class Solution {
11     public int pathSum(TreeNode root, int sum) {
12         if(root == null){
13             return 0;
14         }
15         return dfs(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);
16     }
17     
18     private int dfs(TreeNode root, int sum){
19         int res = 0;
20         if(root == null){
21             return res;
22         }
23         
24         sum -= root.val;
25         if(sum == 0){
26             res++;
27         }
28         res += dfs(root.left, sum) + dfs(root.right, sum);
29         return res;
30     }
31 }

用HashMap来maintain prefix sum. Key 是prefix sum, value 是加到prefix sum的method count.

当cur - sum的值正好出现在prefix中时说明 cur-prefix正好是sum, 从prefix到cur的这一段加起来正好是sum.

Time Complexity: O(n). Space: O(n), hm.size().

AC Java:

 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  */
10 public class Solution {
11     public int pathSum(TreeNode root, int sum) {
12         HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
13         hm.put(0, 1); //设置prefix sum为0的default value是1.
14         return pathSumHelper(root, sum, 0, hm);
15     }
16     
17     private int pathSumHelper(TreeNode root, int sum, int cur, HashMap<Integer, Integer> hm){
18         if(root == null){
19             return 0;
20         }
21         cur += root.val;
22         int res = hm.getOrDefault(cur-sum, 0);
23         
24         hm.put(cur, hm.getOrDefault(cur, 0)+1);
25         res += pathSumHelper(root.left, sum, cur, hm) + pathSumHelper(root.right, sum, cur, hm);
26         hm.put(cur, hm.get(cur)-1); //remove count by 1, backtracking要算其他branch时先回复原值
27         
28         return res;
29     }
30 }

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/6293275.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值