From 541b2ee2af070b207fa8154462570f55cf3521c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A9=E8=BE=95=E9=BE=99=E5=84=BF?= Date: Mon, 11 Apr 2022 13:59:58 +0800 Subject: [PATCH] =?UTF-8?q?270:=E6=9C=80=E6=8E=A5=E8=BF=91=E7=9A=84?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/ClosestBinarySearchTreeValue.java | 80 +++++++++++++++++++ .../content/ClosestBinarySearchTreeValue.md | 22 +++++ 2 files changed, 102 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/ClosestBinarySearchTreeValue.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/ClosestBinarySearchTreeValue.md diff --git a/src/main/java/leetcode/editor/cn/ClosestBinarySearchTreeValue.java b/src/main/java/leetcode/editor/cn/ClosestBinarySearchTreeValue.java new file mode 100644 index 0000000..2cf3933 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/ClosestBinarySearchTreeValue.java @@ -0,0 +1,80 @@ +//给定一个不为空的二叉搜索树和一个目标值 target,请在该二叉搜索树中找到最接近目标值 target 的数值。 +// +// 注意: +// +// +// 给定的目标值 target 是一个浮点数 +// 题目保证在该二叉搜索树中只会存在一个最接近目标值的数 +// +// +// 示例: +// +// 输入: root = [4,2,5,1,3],目标值 target = 3.714286 +// +// 4 +// / \ +// 2 5 +// / \ +//1 3 +// +//输出: 4 +// +// Related Topics 树 深度优先搜索 二叉搜索树 二分查找 二叉树 👍 112 👎 0 + +package leetcode.editor.cn; + +import com.code.leet.entiy.TreeNode; + +//270:最接近的二叉搜索树值 +public class ClosestBinarySearchTreeValue { + public static void main(String[] args) { + Solution solution = new ClosestBinarySearchTreeValue().new Solution(); + // TO TEST + } + //leetcode submit region begin(Prohibit modification and deletion) + + /** + * 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 { + public int closestValue(TreeNode root, double target) { + double left = target; + double right = target; + float diff = 1e-6f; + while (root != null) { + if (Math.abs(root.val - target) < diff) { + return root.val; + } + if (root.val > target) { + right = root.val; + root = root.left; + } else { + left = root.val; + root = root.right; + } + } + + if (Math.abs(left - target) < diff) { + return (int) right; + } else if (Math.abs(right - target) < diff) { + return (int) left; + } else { + return target - left > right - target ? (int) right : (int) left; + } + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/src/main/java/leetcode/editor/cn/doc/content/ClosestBinarySearchTreeValue.md b/src/main/java/leetcode/editor/cn/doc/content/ClosestBinarySearchTreeValue.md new file mode 100644 index 0000000..f186197 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/ClosestBinarySearchTreeValue.md @@ -0,0 +1,22 @@ +

给定一个不为空的二叉搜索树和一个目标值 target,请在该二叉搜索树中找到最接近目标值 target 的数值。

+ +

注意:

+ + + +

示例:

+ +
输入: root = [4,2,5,1,3],目标值 target = 3.714286
+
+    4
+   / \
+  2   5
+ / \
+1   3
+
+输出: 4
+
+
Related Topics
  • 深度优先搜索
  • 二叉搜索树
  • 二分查找
  • 二叉树

  • 👍 112
  • 👎 0
  • \ No newline at end of file