Problem
Given a Binary Search Tree (BST) keyed on positive integers, find the shortest path (a sequence of connected nodes) in the tree such that the sum of the keys in the path adds up to a given integer K
. If no such path exists, return an appropriate message indicating that no path is found. Each path must originate from the root of the tree and may terminate at any node such that the cumulative sum equals K
.
Examples
Example 1
|
|
Example 2
|
|
Solution
Method 1 - DFS
Reasoning or Intuition
Imagine we have a target number K
, and we want to identify the smallest possible set of numbers (path) in a Binary Search Tree (BST) that sum up to K
. The goal is to find the shortest sequence of connected nodes that adds up to this target.
Let us consider an example where K = 25
for the given BST:
|
|
There are two possible paths that sum up to 25
:
- Path 1:
10 → 15
- Cumulative sum = 10 + 15 = 25.
- Path length = 2.
- Path 2:
10 → 7 → 5 → 3
- Cumulative sum = 10 + 7 + 5 + 3 = 25.
- Path length = 4.
Both paths achieve the target sum, but clearly, Path 1 is shorter (length = 2), so it is the preferred solution. The requirement is to find a path that achieves the desired sum while minimising the number of nodes in the sequence.
Key Observation
In a Binary Search Tree:
- Larger numbers are typically located in the right subtrees, while smaller numbers are located in the left subtrees.
- As summing larger numbers reduces the number of nodes needed to reach the target sum, exploring the right subtree preferentially can often produce shorter paths.
Consider the following principle:
- If we first explore the right subtree, we may find shorter paths summing to
K
due to the presence of larger values. - If no valid path is found on the right, we then shift to evaluating the left subtree.
- If the cumulative sum exceeds
K
during traversal, further exploration of that path is unnecessary, as additional nodes will only increase the sum.
Approach
To solve this problem efficiently:
- Prioritise Right Subtrees: Start investigating the right subtrees first, as they often contain larger values.
- Use Recursive Backtracking: Track the cumulative sum and the path so far, and compare results with the shortest valid path.
- Terminate Suboptimal Paths: If the cumulative sum exceeds
K
, stop exploring that branch to save computational effort. - Compare and Update: Whenever a valid path sums to
K
, check if its length is smaller than the shortest known path. If it is, update the result. - Fallback to Left Subtrees: If no valid path exists on the right, shift exploration to left subtrees.
Code
|
|
|
|
Complexity
-
⏰ Time complexity:
O(n)
. Every node in the tree is visited once, wheren
is the number of nodes. -
🧺 Space complexity:
O(h)
. The recursion stack depth is proportional to the height of the tree, which ish
(logarithmic for a balanced BST, andO(N)
for a skewed tree).