Binary Tree Path Sum

Question: 

Given a binary tree, find all paths that sum of the nodes in the path equals to a given number target.

A valid path is from root node to any of the leaf nodes.

Example:

Given a binary tree, and target = 5:

     1
    /    2   4
  /  2   3

return

[
  [1, 2, 2],
  [1, 4]
]

Analysis:

先序遍历整棵树,用一个变量sum记录从根节点到当前节点的节点数字之和,当遇到叶子节点(节点左右子节点均为空)并且和等于目标target的时候,该条路径就是我们要找的路径,将它加入到list当中。

递归函数helper的定义:在当前节点至叶子节点所有的路径中找出符合条件的路径加入到结果集当中。

Code:

 1 public class Solution {
 2     /**
 3      * @param root the root of binary tree
 4      * @param target an integer
 5      * @return all valid paths
 6      */
 7     public List<List<Integer>> binaryTreePathSum(TreeNode root, int target) {
 8         List<List<Integer>> list = new ArrayList<>();
 9         List<Integer> path = new ArrayList<Integer>();
10
11         if(root == null) {
12             return list;
13         }
14
15         path.add(root.val);
16         helper(root, root.val, target, path, list);
17         return list;
18     }
19
20     private void helper(TreeNode root,
21                         int sum,
22                         int target,
23                         List<Integer> path,
24                         List<List<Integer>> list) {
25         if(root.left == null && root.right == null && sum == target) {
26             list.add(new ArrayList<Integer>(path));
27             return;
28         }
29
30         if(root.left != null) {
31             path.add(root.left.val);
32             helper(root.left, sum + root.left.val, target, path, list);
33             path.remove(path.size() - 1);
34         }
35
36         if(root.right != null) {
37             path.add(root.right.val);
38             helper(root.right, sum + root.right.val, target, path, list);
39             path.remove(path.size() - 1);
40         }
41     }
42 }

Complexity:

时间复杂度为O(n),n为树中节点的个数。空间复杂度为O(h * 2^(h - 1)),h为树的高度。

时间: 2024-10-05 04:34:10

Binary Tree Path Sum的相关文章

[LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II

Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Pr

Binary Tree Path Sum Lintcode

Given a binary tree, find all paths that sum of the nodes in the path equals to a given number target. A valid path is from root node to any of the leaf nodes. Have you met this question in a real interview? Yes Example Given a binary tree, and targe

LintCode : Binary Tree Path Sum

Description: Given a binary tree, find all paths that sum of the nodes in the path equals to a given number target. A valid path is from root node to any of the leaf nodes. Example: Given a binary tree, and target = 5: 1 / 2 4 / 2 3 return [ [1, 2, 2

LeetCode[Tree]: 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, return true, as there exist a root-to-leaf pat

LeetCode[Tree]: 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, return 这个题目适合用递归来解,我的C++代码实现如下: class Solution { public: vector<vector<int> > pathSum

[Leetcode] Binary tree--112. Path Sum

112. 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 ret

[Leetcode] Binary tree-- 437. 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

【leetcode】 257. Binary Tree Path

/** * @author johnsondu * @time 2015.8.21 16:30 * @description tranverse a tree * @url https://leetcode.com/problems/binary-tree-paths/ */ /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; *

Lintcode376-Binary Tree Path Sum-Easy

376. Binary Tree Path Sum 中文English Given a binary tree, find all paths that sum of the nodes in the path equals to a given number target. A valid path is from root node to any of the leaf nodes. Example Example 1: Input: {1,2,4,2,3} 5 Output: [[1, 2