import
java.util.*;
class
GFG
{
static
class
Node
{
int
data;
Node left, right;
};
static
Node newNode(
int
data)
{
Node temp =
new
Node();
temp.data = data;
temp.left = temp.right =
null
;
return
temp;
}
static
int
largestUniquePathUtil(Node node, HashMap<Integer,
Integer> m)
{
if
(node ==
null
)
return
m.size();
if
(m.containsKey(node.data))
{
m.put(node.data, m.get(node.data) +
1
);
}
else
{
m.put(node.data,
1
);
}
int
max_path = Math.max(largestUniquePathUtil(node.left, m),
largestUniquePathUtil(node.right, m));
if
(m.containsKey(node.data))
{
m.put(node.data, m.get(node.data) -
1
);
}
if
(m.get(node.data) ==
0
)
m.remove(node.data);
return
max_path;
}
static
int
largestUniquePath(Node node)
{
if
(node ==
null
)
return
0
;
HashMap<Integer,
Integer> hash =
new
HashMap<Integer,
Integer>();
return
largestUniquePathUtil(node, hash);
}
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.left = newNode(
6
);
root.right.right = newNode(
7
);
root.right.left.right = newNode(
8
);
root.right.right.right = newNode(
9
);
System.out.println(largestUniquePath(root));
}
}