using
System;
using
System.Collections;
using
System.Collections.Generic;
public
class
Node{
public
int
data;
public
Node left, right;
public
Node(
int
item){
data = item;
left = right =
null
;
}
}
public
class
BinaryTree{
public
Node root;
public
static
void
helper(Node root, List<
int
> arr, List<List<
int
>> ans){
if
(root ==
null
)
return
;
List<
int
> arr1 =
new
List<
int
>(arr);
arr1.Add(root.data);
if
(root.left ==
null
&& root.right ==
null
){
ans.Add(arr1);
return
;
}
helper(root.left, arr1, ans);
helper(root.right, arr1, ans);
}
public
static
List<List<
int
>> Paths(Node root){
List<List<
int
>> ans =
new
List<List<
int
>>();
if
(root ==
null
)
return
ans;
List<
int
> arr =
new
List<
int
>();
helper(root, arr, ans);
return
ans;
}
public
static
void
Main(
string
[] args){
BinaryTree tree =
new
BinaryTree();
tree.root =
new
Node(10);
tree.root.left =
new
Node(8);
tree.root.right =
new
Node(2);
tree.root.left.left =
new
Node(3);
tree.root.left.right =
new
Node(5);
tree.root.right.left =
new
Node(2);
List<List<
int
>> final =
new
List<List<
int
>>();
final = Paths(tree.root);
for
(
var
i = 0; i<final.Count; i++){
for
(
var
j = 0; j<final[i].Count; j++){
Console.Write(final[i][j] +
" "
);
}
Console.WriteLine(
""
);
}
}
}