《LeetCode力扣练习》代码随想录——二叉树(二叉搜索树的最小绝对差—Java)
刷题思路来源于 代码随想录
530. 二叉搜索树的最小绝对差
-
二叉树-中序遍历
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { private int result = Integer.MAX_VALUE; private TreeNode pre = null; public int getMinimumDifference(TreeNode root) { recursion(root); return result; } private void recursion(TreeNode root) { if (root == null) { return; } recursion(root.left); if (pre != null) { int diff = root.val - pre.val; result = diff < result ? diff : result; } pre = root; recursion(root.right); return; } }