<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p>

<p>Find a pair of indices <code>(i, j)</code> such that:</p>

<ul>
	<li><code>i != j</code>,</li>
	<li><code>abs(i - j) &lt;= indexDiff</code>.</li>
	<li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li>
</ul>

<p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> We can choose (i, j) = (0, 3).
We satisfy the three conditions:
i != j --&gt; 0 != 3
abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3
abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3
<strong>Output:</strong> false
<strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false.
</pre>

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

<ul>
	<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= indexDiff &lt;= nums.length</code></li>
	<li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li>
</ul>

class TreeNode:
    def __init__(self, val: int) -> None:
        self.val = val
        self.left = None
        self.right = None
        

def remove_node(node: TreeNode) -> Optional[TreeNode]:
    if not node:
        return

    if not node.left and not node.right:
        return

    if node.left and not node.right:
        return node.left

    if not node.left and node.right:
        return node.right
    
    # both nodes
    next_node = node.right
    prev_node = None
    while True:
        if next_node.left:
            prev_node = next_node
            next_node = next_node.left
        else:
            break
    
    node.val = next_node.val
    if not prev_node:
        node.right = next_node.right
    else:
        prev_node.left = next_node.right
        
    return node
    

class BST:
    # Надо искать сразу diff в дереве
    def __init__(self):
        self.root = None
        
    def insert(self, val: int) -> None:
        if not self.root:
            self.root = TreeNode(val)
            return
        
        node = self.root
        while node:
            if val < node.val:
                if node.left:
                    node = node.left
                else:
                    node.left = TreeNode(val)
                    return
            else:
                if node.right:
                    node = node.right
                else:
                    node.right = TreeNode(val)
                    return
                
    def create_from_sorted(self, arr: list) -> None:
        self.root = TreeNode(arr[0])
        node = self.root
        
        arr = arr[1:]
        for n in arr:
            node.right = TreeNode(n)
            node = node.right
    
    def remove(self, val: int) -> None:        
        if self.root.val == val:
            self.root = remove_node(self.root)
            return
            
        node = self.root
        while node:
            if node.left and val < node.val:
                if node.left.val == val:
                    node.left = remove_node(node.left)
                    return
                else:
                    node = node.left
            
            elif node.right and val > node.val:
                if node.right.val == val:
                    node.right = remove_node(node.right)
                    return
                else:
                    node = node.right
            
            else:
                print(f"DELETE ERROR {val = } {node.val = }")
                return
            
    def has_diff(self, val: int, diff: int) -> bool:
        node = self.root
        while node:
            d = node.val - val
            if abs(d) <= diff:
                return True
            
            if d < 0:
                node = node.right
            else:
                node = node.left
        
        return False
    
    def find(self, val: int) -> bool:
        node = self.root
        
        while node:
            if node.val == val:
                return True
            
            if val < node.val:
                node = node.left
            else:
                node = node.right
                
        return False
            
                

class Solution:
    def containsNearbyAlmostDuplicate(self, nums: List[int], indexDiff: int, valueDiff: int) -> bool:
        iters = len(nums) - indexDiff
        bst = BST()
        
        to_insert = nums[1:indexDiff + 1][:]
        random.shuffle(to_insert)
        
        # bst.create_from_sorted(sorted())
        for n in to_insert:
            bst.insert(n)
                
        if bst.has_diff(nums[0], valueDiff):
            return True
        
        iters = len(nums) - 1
        for i in range(1, iters):
            bst.remove(nums[i])
            
            if i + indexDiff < len(nums):
                bst.insert(nums[i + indexDiff])
            
            if bst.has_diff(nums[i], valueDiff):
                return True

        return False
        
35640