<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>upper_bound Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/upper_bound/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/upper_bound/</link>
	<description></description>
	<lastBuildDate>Wed, 23 Nov 2022 08:40:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>upper_bound Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/upper_bound/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2476. Closest Nodes Queries in a Binary Search Tree</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-2476-closest-nodes-queries-in-a-binary-search-tree/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-2476-closest-nodes-queries-in-a-binary-search-tree/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 22 Nov 2022 00:28:58 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[BST]]></category>
		<category><![CDATA[lower_bound]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[tree]]></category>
		<category><![CDATA[upper_bound]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9889</guid>

					<description><![CDATA[<p>You are given the&#160;root&#160;of a&#160;binary search tree&#160;and an array&#160;queries&#160;of size&#160;n&#160;consisting of positive integers. Find a&#160;2D&#160;array&#160;answer&#160;of size&#160;n&#160;where&#160;answer[i] = [mini, maxi]: mini&#160;is the&#160;largest&#160;value in the tree that&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-2476-closest-nodes-queries-in-a-binary-search-tree/">花花酱 LeetCode 2476. Closest Nodes Queries in a Binary Search Tree</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 2476. Closest Nodes Queries in a Binary Search Tree - 刷题找工作 EP405" width="500" height="281" src="https://www.youtube.com/embed/UZYgQLMyocw?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>You are given the&nbsp;<code>root</code>&nbsp;of a&nbsp;<strong>binary search tree&nbsp;</strong>and an array&nbsp;<code>queries</code>&nbsp;of size&nbsp;<code>n</code>&nbsp;consisting of positive integers.</p>



<p>Find a&nbsp;<strong>2D</strong>&nbsp;array&nbsp;<code>answer</code>&nbsp;of size&nbsp;<code>n</code>&nbsp;where&nbsp;<code>answer[i] = [min<sub>i</sub>, max<sub>i</sub>]</code>:</p>



<ul><li><code>min<sub>i</sub></code>&nbsp;is the&nbsp;<strong>largest</strong>&nbsp;value in the tree that is smaller than or equal to&nbsp;<code>queries[i]</code>. If a such value does not exist, add&nbsp;<code>-1</code>&nbsp;instead.</li><li><code>max<sub>i</sub></code>&nbsp;is the&nbsp;<strong>smallest</strong>&nbsp;value in the tree that is greater than or equal to&nbsp;<code>queries[i]</code>. If a such value does not exist, add&nbsp;<code>-1</code>&nbsp;instead.</li></ul>



<p>Return&nbsp;<em>the array</em>&nbsp;<code>answer</code>.</p>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2022/09/28/bstreeedrawioo.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> root = [6,2,13,1,4,9,15,null,null,null,null,null,null,14], queries = [2,5,16]
<strong>Output:</strong> [[2,2],[4,6],[15,-1]]
<strong>Explanation:</strong> We answer the queries in the following way:
- The largest number that is smaller or equal than 2 in the tree is 2, and the smallest number that is greater or equal than 2 is still 2. So the answer for the first query is [2,2].
- The largest number that is smaller or equal than 5 in the tree is 4, and the smallest number that is greater or equal than 5 is 6. So the answer for the second query is [4,6].
- The largest number that is smaller or equal than 16 in the tree is 15, and the smallest number that is greater or equal than 16 does not exist. So the answer for the third query is [15,-1].
</pre>



<p><strong>Example 2:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2022/09/28/bstttreee.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> root = [4,null,9], queries = [3]
<strong>Output:</strong> [[-1,4]]
<strong>Explanation:</strong> The largest number that is smaller or equal to 3 in the tree does not exist, and the smallest number that is greater or equal to 3 is 4. So the answer for the query is [-1,4].
</pre>



<p><strong>Constraints:</strong></p>



<ul><li>The number of nodes in the tree is in the range&nbsp;<code>[2, 10<sup>5</sup>]</code>.</li><li><code>1 &lt;= Node.val &lt;= 10<sup>6</sup></code></li><li><code>n == queries.length</code></li><li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= queries[i] &lt;= 10<sup>6</sup></code></li></ul>



<h2><strong>Solution: Convert to sorted array</strong></h2>



<figure class="wp-block-image size-full"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/11/2476-ep405-s1.png"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/11/2476-ep405-s1.png" alt="" class="wp-image-9898" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/11/2476-ep405-s1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/11/2476-ep405-s1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/11/2476-ep405-s1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></figure>



<figure class="wp-block-image size-full"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/11/2476-ep405-s2.png"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/11/2476-ep405-s2.png" alt="" class="wp-image-9899" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/11/2476-ep405-s2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/11/2476-ep405-s2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/11/2476-ep405-s2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></figure>



<figure class="wp-block-image size-full"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/11/2476-ep405-s3.png"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/11/2476-ep405-s3.png" alt="" class="wp-image-9900" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/11/2476-ep405-s3.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/11/2476-ep405-s3-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/11/2476-ep405-s3-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></figure>



<figure class="wp-block-image size-full"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/11/2476-ep405-s4.png"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/11/2476-ep405-s4.png" alt="" class="wp-image-9901" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/11/2476-ep405-s4.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/11/2476-ep405-s4-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/11/2476-ep405-s4-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></figure>



<p>Since we don&#8217;t know whether the tree is balanced or not, the safest and easiest way is to convert the tree into a sorted array using inorder traversal. Or just any traversal and sort the array later on.</p>



<p>Once we have a sorted array, we can use lower_bound / upper_bound to query.</p>



<p>Time complexity: O(qlogn)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; closestNodes(TreeNode* root, vector&lt;int&gt;&amp; queries) {    
    vector&lt;int&gt; vals;
    function&lt;void(TreeNode*)&gt; inorder = [&amp;](TreeNode* root) {
      if (!root) return;
      inorder(root-&gt;left);
      vals.push_back(root-&gt;val);
      inorder(root-&gt;right);
    };
    
    inorder(root);
    
    vector&lt;vector&lt;int&gt;&gt; ans;
    for (const int q : queries) {
      vector&lt;int&gt; cur{-1, -1};
      auto lit = upper_bound(begin(vals), end(vals), q);
      if (lit != begin(vals))
        cur[0] = *(prev(lit));
      auto rit = lower_bound(begin(vals), end(vals), q);
      if (rit != end(vals))
        cur[1] = *rit;      
      ans.push_back(std::move(cur));
    }
    return ans;
  }
};</pre>
</div></div>



<p>One binary search per query.</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; closestNodes(TreeNode* root, vector&lt;int&gt;&amp; queries) {    
    vector&lt;int&gt; vals;
    function&lt;void(TreeNode*)&gt; inorder = [&amp;](TreeNode* root) {
      if (!root) return;
      inorder(root-&gt;left);
      if (vals.empty() || root-&gt;val &gt; vals.back()) vals.push_back(root-&gt;val);
      inorder(root-&gt;right);
    };
    
    inorder(root);
    
    vector&lt;vector&lt;int&gt;&gt; ans;
    for (const int q : queries) {
      vector&lt;int&gt; cur{-1, -1};      
      auto it = lower_bound(begin(vals), end(vals), q);
      if (it != end(vals) &amp;&amp; *it == q)
        cur[0] = cur[1] = q;
      else {
        if (it != begin(vals)) cur[0] = *prev(it);
        if (it != end(vals)) cur[1] = *it;        
      }
      ans.push_back(std::move(cur));
    }
    return ans;
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-2476-closest-nodes-queries-in-a-binary-search-tree/">花花酱 LeetCode 2476. Closest Nodes Queries in a Binary Search Tree</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-2476-closest-nodes-queries-in-a-binary-search-tree/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
