[LeetCode] 437. Path Sum III_ Easy tag: DFS

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

这个题目的思路就是类似于[LeetCode] 113. Path Sum II, 只不过我们不需要一定是leaf再判断, 同时recursive root.left and root.right, 最后返回总的个数即可.

1. Constraints1) empty => 0

2. IDeas

DFS     T: O(n^2)  worst cases, when like linked lists        T: O(nlgn)  best cases, when balanced tree   because height = lgn

1) edge case, if not root: return 02) create a helper function, get number of paths from root -> any child node s.t sum(path) == target3) return helper(root) and recursively call root.left and root.right

3. Code
1 class Solution:
2     def pathSum3(self, root, target):
3         def rootSum(root, target):  # helper function to get number of paths from root -> any child node s.t sum == target
4             if not root: return 0
5             d = target - root.val
6             temp = 1 if d == 0 else 0
7             return temp + rootSum(root.left, d) + rootSum(root.right, d)
8         if not root: return 0
9         return rootSum(root) + self.pathSum3(root.left, target) + self.pathSum3(root.right, target)

4. Test cases

1) empty

2) 1, 1

3)

1
     /      1   1
               target = 14)
target = 8
      10
     /      5   -3
   / \      3   2   11
 / \   3  -2   1

原文地址:https://www.cnblogs.com/Johnsonxiong/p/9326919.html

时间: 2024-10-11 11:58:05

[LeetCode] 437. Path Sum III_ Easy tag: DFS的相关文章

437. Path Sum III - Easy

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

[LeetCode] 257. Binary Tree Paths_ Easy tag: DFS

Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example: Input: 1 / 2 3 5 Output: ["1->2->5", "1->3"] Explanation: All root-to-leaf paths are: 1->2->5, 1->3 思路为DFS, 只是appen

LeetCode "437. Path Sum III"

The punch line of this one: sum of leaves: pi..pj = root..pj - root..pi. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class

LeetCode 437. Path Sum III(统计路径和等于sum的路径数量)

题意:统计路径和等于sum的路径数量. (1)节点值可正可负 (2)路径两端不一定是根结点或叶子结点 (3)路径一定是向下 分析:路径起点 (1)位于root(统计以root开头的和等于sum的路径数量) (2)位于root->left子树(递归) (3)位于root->right子树(递归) /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode

LeetCode:Path Sum - 树的根节点到叶节点的数字之和

1.题目名称 Path Sum(树的根节点到叶节点的数字之和) 2.题目地址 https://leetcode.com/problems/path-sum/ 3.题目内容 英文:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. 中文:给定一颗二叉树,如

【Leetcode】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 and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 思路:与[Leetcode]Path Sum 不同

LeetCode "Minimum Path Sum" - 2D DP

An intuitive 2D DP: dp[i][j] = min(grid[i-1][j-1] + dp[i-1][j], grid[i-1][j-1] + dp[i][j+1]) class Solution { public: int minPathSum(vector<vector<int> > &grid) { // dp[i][j] = min(dp[i-1][j] + dp[i][j], dp[i][j-1] + dp[i][j]); int n = gri

【LeetCode】Path Sum

题目 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true,

Leetcode:Minimum Path Sum 矩形网格最小路径和

Minimum Path Sum: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 解题分析: 每次只能向下或者向