From b6bf3b60ff3000b54278cff054d33acdd1bb45c8 Mon Sep 17 00:00:00 2001 From: Joshua <47053655+Joshua-Lu@users.noreply.github.com> Date: Fri, 14 May 2021 01:24:15 +0800 Subject: [PATCH] =?UTF-8?q?Update=200530.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E7=BB=9D=E5=AF=B9?= =?UTF-8?q?=E5=B7=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0530.二叉搜索树的最小绝对差 Java版本 --- .../0530.二叉搜索树的最小绝对差.md | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index d5abc692..e30e76f5 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -151,7 +151,29 @@ public: Java: - +```Java +class Solution { + TreeNode pre;// 记录上一个遍历的结点 + int result = Integer.MAX_VALUE; + public int getMinimumDifference(TreeNode root) { + if (root == null) { + return result; + } + // 左 + int left = getMinimumDifference(root.left); + + // 中 + if (pre != null) { + result = Math.min(left, root.val - pre.val); + } + pre = root; + // 右 + int right = getMinimumDifference(root.right); + result = Math.min(right, result); + return result; + } +} +``` Python: