using System.Collections.Generic;
static Node getNode(int data)
Node newNode = new Node();
newNode.left = newNode.right = null;
static Boolean getPath(Node root, List<int> arr, int x)
if (getPath(root.left, arr, x) || getPath(root.right, arr, x))
arr.RemoveAt(arr.Count-1);
static void printPathBetweenNodes(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);
while (i != path1.Count || j != path2.Count)
if (i == j && path1[i] == path2[i])
for ( i = path1.Count - 1; i > intersection; i--)
Console.Write( path1[i] + " ");
for ( i = intersection; i < path2.Count; i++)
Console.Write( path2[i] + " ");
public static void Main(String[] args)
root.left.left = getNode(3);
root.left.left.left = getNode(7);
root.left.right = getNode(4);
root.left.right.left = getNode(8);
root.left.right.right = getNode(9);
root.right.left = getNode(5);
root.right.right = getNode(6);
printPathBetweenNodes(root, node1, node2);