using
System;
using
System.Collections.Generic;
class
Graph
{
public
class
Node
{
public
int
key;
public
Node left, right;
}
public
static
Node newNode(
int
key)
{
Node temp =
new
Node();
temp.key = key;
temp.left = temp.right =
null
;
return
(temp);
}
public
static
void
printPathsUtil(Node curr_node,
int
sum,
int
sum_so_far,
List<
int
> path)
{
if
(curr_node ==
null
)
return
;
sum_so_far += curr_node.key;
path.Add(curr_node.key);
if
(sum_so_far == sum)
{
Console.Write(
"Path found: "
);
for
(
int
i = 0; i < path.Count; i++)
Console.Write(path[i] +
" "
);
Console.WriteLine();
}
if
(curr_node.left !=
null
)
printPathsUtil(curr_node.left, sum,
sum_so_far, path);
if
(curr_node.right !=
null
)
printPathsUtil(curr_node.right, sum,
sum_so_far, path);
path.RemoveAt(path.Count - 1);
}
public
static
void
printPaths(Node root,
int
sum)
{
List<
int
> path =
new
List<
int
>();
printPathsUtil(root, sum, 0, path);
}
public
static
void
Main(
string
[] args)
{
Node root = newNode(10);
root.left = newNode(28);
root.right = newNode(13);
root.right.left = newNode(14);
root.right.right = newNode(15);
root.right.left.left = newNode(21);
root.right.left.right = newNode(22);
root.right.right.left = newNode(23);
root.right.right.right = newNode(24);
int
sum = 38;
printPaths(root, sum);
}
}