{"id":22513965,"url":"https://github.com/emahtab/closest-binary-search-tree-value","last_synced_at":"2026-03-19T23:03:04.678Z","repository":{"id":79525363,"uuid":"239304048","full_name":"eMahtab/closest-binary-search-tree-value","owner":"eMahtab","description":"Closest Binary Search Tree Value","archived":false,"fork":false,"pushed_at":"2024-10-18T13:49:32.000Z","size":11,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-02T03:26:15.856Z","etag":null,"topics":["binary-search-tree","leetcode","problem-solving"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-09T12:51:13.000Z","updated_at":"2024-10-18T13:49:35.000Z","dependencies_parsed_at":"2025-02-02T03:25:35.589Z","dependency_job_id":"178c46bd-ee18-4daf-932d-d8251ae1fade","html_url":"https://github.com/eMahtab/closest-binary-search-tree-value","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fclosest-binary-search-tree-value","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fclosest-binary-search-tree-value/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fclosest-binary-search-tree-value/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fclosest-binary-search-tree-value/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/closest-binary-search-tree-value/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245952330,"owners_count":20699470,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binary-search-tree","leetcode","problem-solving"],"created_at":"2024-12-07T03:15:17.983Z","updated_at":"2026-02-09T09:33:02.488Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Closest Binary Search Tree Value\n## https://leetcode.com/problems/closest-binary-search-tree-value\n\nGiven a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.\n\n**Note:**\n1. Given target value is a floating point.\n2. You are guaranteed to have only one unique value in the BST that is closest to the target.\n```\nExample:\n\nInput: root = [4,2,5,1,3], target = 3.714286\n\n    4\n   / \\\n  2   5\n / \\\n1   3\n\nOutput: 4\n\nBut if target is 3.5 and we are asked to return the smallest number which is closest to target,\nthen answer will be 3 and not 4 (both 3 and 4 have the same difference from target but 3 is smaller than 4) .\n```\n# Implementation 1 : O(nlogn), inorder traversal, sort \n```java\nclass Solution {\n  public int closestValue(TreeNode root, double target) {\n    List\u003cInteger\u003e nums = new ArrayList();\n    inorder(root, nums);\n    Collections.sort(nums, (n1,n2) -\u003e \n                      Double.compare(Math.abs(n1 - target), Math.abs(n2 - target)));\n    return nums.get(0);\n  }\n\n  public void inorder(TreeNode node, List\u003cInteger\u003e nums) {\n    if (node == null) return;\n    inorder(node.left, nums);\n    nums.add(node.val);\n    inorder(node.right, nums);\n  }\n}\n```\n\n# Implementation 2 : O(h)\n\n```java\nclass Solution {\n    public int closestValue(TreeNode root, double target) {\n        int closestValue = root.val;\n        TreeNode node = root;\n        while(node != null){\n            if(Math.abs(node.val - target) \u003c Math.abs(closestValue - target))\n                closestValue = node.val;\n            node = target \u003c node.val ? node.left : node.right;\n        }\n        return closestValue;\n    }\n}\n```\n# Implementation 3 : O(h), If there are multiple answers, return the smallest number\n```java\nclass Solution {\n    public int closestValue(TreeNode root, double target) {\n        int closestValue = root.val;\n        TreeNode node = root;\n        while(node != null){\n            if(Math.abs(node.val - target) \u003c Math.abs(closestValue - target) ||\n               (Math.abs(node.val - target) == Math.abs(closestValue - target) \u0026\u0026 node.val \u003c closestValue))\n                closestValue = node.val;\n            node = target \u003c node.val ? node.left : node.right;\n        }\n        return closestValue;\n    }\n}\n```\n\n# References :\nhttps://www.youtube.com/watch?v=_sz0Y4g1Gos\n\nhttps://leetcode.com/problems/closest-binary-search-tree-value/editorial\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fclosest-binary-search-tree-value","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fclosest-binary-search-tree-value","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fclosest-binary-search-tree-value/lists"}