Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find paths in a binary search tree summing to a target value

Given a binary search tree and a target value, find all the paths (if there exists more than one) which sum up to the target value. It can be any path in the tree. It doesn't have to be from the root.

For example, in the following binary search tree:

  2
 / \ 
1   3 

when the sum should be 6, the path 1 -> 2 -> 3 should be printed.

like image 526
TimeToCodeTheRoad Avatar asked Jan 04 '11 08:01

TimeToCodeTheRoad


People also ask

How do you find the path sum?

Start from the root node of the Binary tree with the initial path sum of 0. Add the value of the current node to the path sum. Travel to the left and right child of the current node with the present value of the path sum. Repeat Step 2 and 3 for all the subsequent nodes of the binary tree.

What is path sum in binary tree?

The path sum of a path is the sum of the node's values in the path. Given the root of a binary tree, return the maximum path sum of any non-empty path. Example 1: Input: root = [1,2,3] Output: 6 Explanation: The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.

What is path sum?

We have to find if any path from the root to leaf has a sum equal to the SUM. Path sum is defined as the sum of all the nodes present in root from root to any leaf node.

How many paths does a binary tree have?

Given a binary tree, return all paths from root to leaf. There are four leafs in the given graph, so we have four paths: from the root to every leaf. Each path is a list of the values from the tree nodes on the path, and we have four lists.


1 Answers

Traverse through the tree from the root and do a post-order gathering of all path sums. Use a hashtable to store the possible paths rooted at a node and going down-only. We can construct all paths going through a node from itself and its childrens' paths.

Here is psuedo-code that implements the above, but stores only the sums and not the actual paths. For the paths themselves, you need to store the end node in the hashtable (we know where it starts, and there's only one path between two nodes in a tree).

function findsum(tree, target)
  # Traverse the children
  if tree->left
    findsum(tree.left, target)
  if tree->right
    findsum(tree.right, target)

  # Single node forms a valid path
  tree.sums = {tree.value}

  # Add this node to sums of children
  if tree.left
    for left_sum in tree.left.sums
      tree.sums.add(left_sum + tree.value)
  if tree.right
    for right_sum in tree.right.sums
      tree.sums.add(right_sum + tree.value)

  # Have we formed the sum?
  if target in tree.sums
    we have a path

  # Can we form the sum going through this node and both children?
  if tree.left and tree.right
    for left_sum in tree.left.sums
      if target - left_sum in tree.right.sums
        we have a path

  # We no longer need children sums, free their memory
  if tree.left
    delete tree.left.sums
  if tree.right
    delete tree.right.sums

This doesn't use the fact that the tree is a search tree, so it can be applied to any binary tree.

For large trees, the size of the hashtable will grow out of hand. If there are only positive values, it could be more efficient to use an array indexed by the sum.

like image 139
moinudin Avatar answered Nov 14 '22 23:11

moinudin