Skip to content

Commit

Permalink
add post : eng_youtube_john_and_jeffray_all_in_summit_2024.md
Browse files Browse the repository at this point in the history
  • Loading branch information
no5ix committed Dec 18, 2024
1 parent 752729e commit e96dcc8
Show file tree
Hide file tree
Showing 7 changed files with 6,304 additions and 6 deletions.
1 change: 0 additions & 1 deletion source/_posts/a_fish_flock_ai_plugin_for_ue4.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ tags:
- CPP
categories:
- GitHub
top: 1
---


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ tags:
- UE4
categories:
- GitHub
top: 5
---


Expand Down
101 changes: 99 additions & 2 deletions source/_posts/algo_meta_top_100.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ According to the [**definition of LCA on Wikipedia**](https://en.wikipedia.org/

**Example 1:**
![alt text](/img/algo_meta_top_100/image-1.png)
!https://assets.leetcode.com/uploads/2018/12/14/binarytree.png

```
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Expand All @@ -147,7 +146,6 @@ Explanation: The LCA of nodes 5 and 1 is 3.

**Example 2:**
![alt text](/img/algo_meta_top_100/image-2.png)
!https://assets.leetcode.com/uploads/2018/12/14/binarytree.png

```
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Expand Down Expand Up @@ -585,3 +583,102 @@ class Solution {
}
}
```


### lc270 - Closest Binary Search Tree Value

- difficulty: Easy
- tags:
- Tree
- Depth-First Search
- Binary Search Tree
- Binary Search
- Binary Tree
- https://leetcode.com/problems/closest-binary-search-tree-value


<p>Given the <code>root</code> of a binary search tree and a <code>target</code> value, return <em>the value in the BST that is closest to the</em> <code>target</code>. If there are multiple answers, print the smallest.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

![alt text](/img/algo_meta_top_100/image-6.png)

<pre>
<strong>Input:</strong> root = [4,2,5,1,3], target = 3.714286
<strong>Output:</strong> 4
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> root = [1], target = 4.428571
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
<li><code>0 &lt;= Node.val &lt;= 10<sup>9</sup></code></li>
<li><code>-10<sup>9</sup> &lt;= target &lt;= 10<sup>9</sup></code></li>
</ul>


#### Solution 1: Recursion

We define a recursive function `dfs(node)`, which starts from the current node `node` and finds the node closest to the target value `target`. We can update the answer by comparing the absolute difference between the current node's value and the target value. If the target value is less than the current node's value, we recursively search the left subtree; otherwise, we recursively search the right subtree.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the number of nodes in the binary search tree.

```java
class Solution {
private int ans;
private double target;
private double diff = Double.MAX_VALUE;

public int closestValue(TreeNode root, double target) {
this.target = target;
dfs(root);
return ans;
}

private void dfs(TreeNode node) {
if (node == null) {
return;
}
double nxt = Math.abs(node.val - target);
if (nxt < diff || (nxt == diff && node.val < ans)) {
diff = nxt;
ans = node.val;
}
node = target < node.val ? node.left : node.right;
dfs(node);
}
}
```

#### Solution 2: Iteration

We can rewrite the recursive function in an iterative form, using a loop to simulate the recursive process. We start from the root node and check whether the absolute difference between the current node's value and the target value is less than the current minimum difference. If it is, we update the answer. Then, based on the size relationship between the target value and the current node's value, we decide to move to the left subtree or the right subtree. The loop ends when we traverse to a null node.

The time complexity is $O(n)$, where $n$ is the number of nodes in the binary search tree. The space complexity is $O(1)$.

```java
class Solution {
public int closestValue(TreeNode root, double target) {
int ans = root.val;
double diff = Double.MAX_VALUE;
while (root != null) {
double nxt = Math.abs(root.val - target);
if (nxt < diff || (nxt == diff && root.val < ans)) {
diff = nxt;
ans = root.val;
}
root = target < root.val ? root.left : root.right;
}
return ans;
}
}
```
Loading

0 comments on commit e96dcc8

Please sign in to comment.