![JAR search and dependency download from the Maven repository](/logo.png)
g0501_0600.s0530_minimum_absolute_difference_in_bst.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0501_0600.s0530_minimum_absolute_difference_in_bst;
// #Easy #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree #Binary_Search_Tree
import com_github_leetcode.TreeNode;
public class Solution {
private int ans = Integer.MAX_VALUE;
private int prev = Integer.MAX_VALUE;
public int getMinimumDifference(TreeNode root) {
if (root == null) {
return ans;
}
getMinimumDifference(root.left);
ans = Math.min(ans, Math.abs(root.val - prev));
prev = root.val;
getMinimumDifference(root.right);
return ans;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy