Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.0270 #1557

Merged
merged 3 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: add js solution to lc problem: No.270
  • Loading branch information
thinkasany committed Aug 31, 2023
commit cb9748a5997e9c822208c88fca70cc072f56b1b8
34 changes: 34 additions & 0 deletions solution/0200-0299/0270.Closest Binary Search Tree Value/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,40 @@ var closestValue = function (root, target) {
};
```

```js
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} target
* @return {number}
*/
var closestValue = function (root, target) {
let mi = Infinity,
ans = root.val;
const dfs = (root, target) => {
if (!root) {
return;
}
dfs(root.left, target);
const t = Math.abs(root.val - target);
if (t < mi) {
mi = t;
ans = root.val;
}
dfs(root.right, target);
};
dfs(root, target);
return ans;
};
```

### **C++**

```cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,40 @@ var closestValue = function (root, target) {
};
```

```js
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} target
* @return {number}
*/
var closestValue = function (root, target) {
let mi = Infinity,
ans = root.val;
const dfs = (root, target) => {
if (!root) {
return;
}
dfs(root.left, target);
const t = Math.abs(root.val - target);
if (t < mi) {
mi = t;
ans = root.val;
}
dfs(root.right, target);
};
dfs(root, target);
return ans;
};
```

### **C++**

```cpp
Expand Down