# Java recursive solution

• Three condition

• left.val==right.val: we output the smaller result if they both have second smaller value, or we output the larger one.

• left.val>right.val: if right node has a second large, compare with left.val, or we just use left.val.

• right.val>left.val: Same solution.

Here is the code:

``````    public int findSecondMinimumValue(TreeNode root) {
int r1 = -1,res = -1;
if(root==null||root.left==null){
return res;
}
if(root.left.val==root.right.val){
r1 = findSecondMinimumValue(root.left);
res = findSecondMinimumValue(root.right);
if(r1!=-1&&res!=-1){
res = Math.min(r1,res);
}
else{
res = Math.max(r1,res);
}
}
else if(root.left.val==root.val){
res = findSecondMinimumValue(root.left);
res = res==-1?root.right.val:Math.min(res,root.right.val);
}
else{
res = findSecondMinimumValue(root.right);
res = res==-1?root.left.val:Math.min(res,root.left.val);
}
return res;
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.