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],
  [1, 4]
]

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root the root of binary tree
     * @param target an integer
     * @return all valid paths
     */
    private List<List<Integer>> result = new ArrayList<>();
    private ArrayList<Integer> path = new ArrayList<Integer>();
    public List<List<Integer>> binaryTreePathSum(TreeNode root, int target) {
        // Write your code here
        // handel corner cases
        if (root == null) {
            return result;
        }
        path.add(root.val);
        helper(root, path, target - root.val);
        return result;
    }

    private void helper(TreeNode node, ArrayList<Integer> path, int target) {
        if (node.left == null && node.right == null) {
            if (target == 0) {
                result.add(new ArrayList<Integer>(path));
            }
            return;
        }
        if (node.left != null) {
            path.add(node.left.val);
            helper(node.left, path, target - node.left.val);
            path.remove(path.size() - 1);
        }
        if (node.right != null) {
            path.add(node.right.val);
            helper(node.right, path, target - node.right.val);
            path.remove(path.size() - 1);
        }
    }
}

Use the divide and conquer method, the most important thing here is to remove the last value after you have go deep to root.left or root.right, and also remember that if node.left == null and node.right == null, then it‘s time to return.

时间: 2025-01-16 13:50:06

LintCode : 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

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],

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

LintCode Binary Tree Maximum Path Sum

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. Example Given the below binary tree: 1 / 2 3 return 6. For this problem we need to think about the problem in this way. Now we want the largest sum of

[lintcode] Binary Tree Maximum Path Sum II

Given a binary tree, find the maximum path sum from root. The path may end at any node in the tree and contain at least one node in it. 给一棵二叉树,找出从根节点出发的路径中,和最大的一条. 这条路径可以在任何二叉树中的节点结束,但是必须包含至少一个点(也就是根了). /** * Definition of TreeNode: * public class Tr

[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