# Easy to understand Java recursive solution

• /**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int getMinimumDifference(TreeNode root) {
int diffLeft = Integer.MAX_VALUE;
int diffRight = Integer.MAX_VALUE;
if(root.left == null && root.right == null) return Math.min(diffLeft, diffRight);
if (root.left != null) {
diffLeft = Math.min(getMin(root, root.left, diffLeft),getMinimumDifference(root.left));
}
if (root.right != null) {
diffRight = Math.min(getMin(root, root.right, diffRight),getMinimumDifference(root.right));
}
return Math.min(diffLeft, diffRight);
}

private int getMin(TreeNode first, TreeNode second, int diff) {
if(first == null || second == null) {
return diff;
}
else {
diff = Math.abs(first.val - second.val);
if(second.left != null) {
diff = Math.min(diff, getMin(first, second.left, diff));
}
if(second.right != null) {
diff = Math.min(diff, getMin(first, second.right, diff));
}
return diff;
}
}
}

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