The following is an interview question.
You are given a binary tree (not necessarily BST) in which each node contains a value. Design an algorithm t
Here is an approach with nlogn
complexity.
Hashmap<CumulativeSum, reference to the corresponding node>
.SUM
.SUM-K
in the HashMap
.HashMap
.Based on Christian's answer above:
public void printSums(Node n, int sum, int currentSum, String buffer) {
if (n == null) {
return;
}
int newSum = currentSum + n.val;
String newBuffer = buffer + " " + n.val;
if (newSum == sum) {
System.out.println(newBuffer);
}
printSums(n.left, sum, newSum, newBuffer);
printSums(n.right, sum, newSum, newBuffer);
printSums(n.left, sum, 0, "");
printSums(n.right, sum, 0, "");
}
printSums(root, targetSum, 0, "");
Since we need the paths having sum == k . I assume worst case complexity can be O(total_paths_in_tree) .
So why not generate every path and check for the sum , anyways it is a tree having negative numbers and is not even a binary search tree .
struct node{
int val;
node *left,*right;
node(int vl)
{
val = vl;
left = NULL;
right = NULL;
}
};
vector<vector<int> > all_paths;
vector<vector<int> > gen_paths(node* root)
{
if(root==NULL)
{
return vector<vector<int> > ();
}
vector<vector<int> > left_paths = gen_paths(root->left);
vector<vector<int> > right_paths = gen_paths(root->right);
left_paths.push_back(vector<int> ()); //empty path
right_paths.push_back(vector<int> ());
vector<vector<int> > paths_here;
paths_here.clear();
for(int i=0;i<left_paths.size();i++)
{
for(int j=0;j<right_paths.size();j++)
{
vector<int> vec;
vec.clear();
vec.insert(vec.end(), left_paths[i].begin(), left_paths[i].end());
vec.push_back(root->val);
vec.insert(vec.end(), right_paths[j].begin(), right_paths[j].end());
paths_here.push_back(vec);
}
}
all_paths.insert(all_paths.end(),paths_here.begin(),paths_here.end());
vector<vector<int> > paths_to_extend;
paths_to_extend.clear();
for(int i=0;i<left_paths.size();i++)
{
paths_to_extend.push_back(left_paths[i]);
paths_to_extend[i].push_back(root->val);
}
for(int i=0;i<right_paths.size();i++)
{
paths_to_extend.push_back(right_paths[i]);
paths_to_extend[paths_to_extend.size()-1].push_back(root->val);
}
return paths_to_extend;
}
For generating paths I have generated all left paths and all right paths And added the left_paths + node->val + right_paths to all_paths at each node. And have sent the paths which can still be extended .i.e all paths from both sides + node .
https://codereview.stackexchange.com/questions/74957/find-all-the-paths-of-tree-that-add-to-a-input-value
I have attempted an answer, expecting code review. My code as well as the reviewers should be helpful source.
public void printPath(N n) {
printPath(n,n.parent);
}
private void printPath(N n, N endN) {
if (n == null)
return;
if (n.left == null && n.right == null) {
do {
System.out.print(n.value);
System.out.print(" ");
} while ((n = n.parent)!=endN);
System.out.println("");
return;
}
printPath(n.left, endN);
printPath(n.right, endN);
}
You can print tree path end the n node. like this printPath(n);
One can reduce this tree to a weighted graph G, where each edge weight = sum of values in each of its nodes.
Then, run Floyd-Warshall algorithm on the graph G. By inspecting elements in the resulting matrix, we can get all pairs of nodes between which the total sum is equal to the desired sum.
Also, note that the shortest path the algorithm gives is also the only path between 2 nodes in this tree.
This is just another approach, not as efficient as a recursive approach.