using
System;
using
System.Collections;
public
class
GFG {
static
int
count = 0;
class
Node {
public
Node left;
public
int
val;
public
Node right;
public
Node(
int
val)
{
this
.right =
null
;
this
.val = val;
this
.left =
null
;
}
}
static
void
cntEvenPath(Node root,
int
sum, Stack path)
{
sum += root.val;
path.Push(root.val);
if
((sum & 1) == 0
&& (root.left ==
null
&& root.right ==
null
)) {
count++;
Console.Write(
"Sum Of ["
);
Object[] arr = path.ToArray();
for
(
int
i = arr.Length - 1; i >= 0; i--) {
if
(i == 0) {
Console.Write(arr[i]);
break
;
}
Console.Write(arr[i] +
","
);
}
Console.WriteLine(
"] = "
+ sum);
return
;
}
if
(root.left !=
null
) {
cntEvenPath(root.left, sum, path);
path.Pop();
}
if
(root.right !=
null
) {
cntEvenPath(root.right, sum, path);
path.Pop();
}
}
static
public
void
Main()
{
Node root =
new
Node(1);
root.right =
new
Node(19);
root.right.right =
new
Node(25);
root.right.left =
new
Node(4);
root.right.left.left =
new
Node(2);
root.right.left.right =
new
Node(9);
root.right.right.left =
new
Node(23);
root.right.right.right =
new
Node(35);
root.right.left.left.right =
new
Node(3);
root.right.left.right.left =
new
Node(7);
root.right.left.right.right =
new
Node(13);
root.right.left.right.right.right =
new
Node(18);
root.right.left.right.right.right.left
=
new
Node(16);
Console.WriteLine();
Stack path =
new
Stack();
cntEvenPath(root, 0, path);
if
(count == 0)
Console.WriteLine(
"No path"
);
}
}