using
System;
using
System.Collections.Generic;
class
GFG
{
public
class
Node
{
public
int
data;
public
Node left;
public
Node right;
}
static
Node newNode(
int
data)
{
Node node =
new
Node();
node.data = data;
node.left =
null
;
node.right =
null
;
return
node;
}
static
Boolean getPath(Node root,
List<
int
> arr,
int
x)
{
if
(root ==
null
)
return
false
;
arr.Add(root.data);
if
(root.data == x)
return
true
;
if
(getPath(root.left, arr, x) ||
getPath(root.right, arr, x))
return
true
;
arr.RemoveAt(arr.Count - 1);
return
false
;
}
static
int
sumOddNodes(Node root,
int
n1,
int
n2)
{
List<
int
> path1 =
new
List<
int
>();
List<
int
> path2 =
new
List<
int
>();
getPath(root, path1, n1);
getPath(root, path2, n2);
int
intersection = -1;
int
i = 0, j = 0;
while
(i < path1.Count || j < path2.Count)
{
if
( i == j && path1[i] == path2[j])
{
i++;
j++;
}
else
{
intersection = j - 1;
break
;
}
}
int
sum = 0;
for
(i = path1.Count - 1; i > intersection; i--)
if
(path1[i] % 2 != 0)
sum += path1[i];
for
(i = intersection; i < path2.Count; i++)
if
(path2[i] % 2 != 0)
sum += path2[i];
return
sum;
}
public
static
void
Main(String []args)
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.right = newNode(6);
int
node1 = 5;
int
node2 = 6;
Console.Write(sumOddNodes(root, node1, node2));
}
}