1 2 3 4 5 6 7 8 9 10 115 | g v U 8 S 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*U { ( O* * DefP o j #inition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x# 2 o ` Y M; } * }( b o ; * 2 6 */ classSolutionE E z + 4 p N 8{ privateint prev = -1; privateint answer = Integer.MAX_VALUE; publicd r y U 1 R +intgetMinimumDiffew - $ - l V Prence(TreeNode root){ if (rh G z u k @oot.left != nb o X hull) { getMinimumDifferem N 5 n ? L R Pnce(root.left); }
if (root.right != null) { getMinimumDifferb d r bence(root.right); } return answer; }2 _ A ( U P 3 b }
Runtime: 1 ms, faster than 95.95% of Java online submissions for Minimum Absolute Difference in BST. Memory Usage:G . B 7 } : U 38.4 MB, less than 97.37% of Java online submissions for Minimum Absolute Di, 1 u F Q : rfference in BST.