<?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>in order Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/in-order/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/in-order/</link>
	<description></description>
	<lastBuildDate>Mon, 29 Nov 2021 05:53:17 +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>in order Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/in-order/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 173. Binary Search Tree Iterator</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-173-binary-search-tree-iterator/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-173-binary-search-tree-iterator/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 29 Nov 2021 05:18:12 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[in order]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[simulation]]></category>
		<category><![CDATA[stack]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8908</guid>

					<description><![CDATA[<p>Implement the&#160;BSTIterator&#160;class that represents an iterator over the&#160;in-order traversal&#160;of a binary search tree (BST): BSTIterator(TreeNode root)&#160;Initializes an object of the&#160;BSTIterator&#160;class. The&#160;root&#160;of the BST is given&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-173-binary-search-tree-iterator/">花花酱 LeetCode 173. Binary Search Tree Iterator</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Implement the&nbsp;<code>BSTIterator</code>&nbsp;class that represents an iterator over the&nbsp;<strong><a href="https://en.wikipedia.org/wiki/Tree_traversal#In-order_(LNR)" target="_blank" rel="noreferrer noopener">in-order traversal</a></strong>&nbsp;of a binary search tree (BST):</p>



<ul><li><code>BSTIterator(TreeNode root)</code>&nbsp;Initializes an object of the&nbsp;<code>BSTIterator</code>&nbsp;class. The&nbsp;<code>root</code>&nbsp;of the BST is given as part of the constructor. The pointer should be initialized to a non-existent number smaller than any element in the BST.</li><li><code>boolean hasNext()</code>&nbsp;Returns&nbsp;<code>true</code>&nbsp;if there exists a number in the traversal to the right of the pointer, otherwise returns&nbsp;<code>false</code>.</li><li><code>int next()</code>&nbsp;Moves the pointer to the right, then returns the number at the pointer.</li></ul>



<p>Notice that by initializing the pointer to a non-existent smallest number, the first call to&nbsp;<code>next()</code>&nbsp;will return the smallest element in the BST.</p>



<p>You may assume that&nbsp;<code>next()</code>&nbsp;calls will always be valid. That is, there will be at least a next number in the in-order traversal when&nbsp;<code>next()</code>&nbsp;is called.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2018/12/25/bst-tree.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input</strong>
["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"]
[[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
<strong>Output</strong>
</pre>


<p>[null, 3, 7, true, 9, true, 15, true, 20, false]</p>



<p><strong>Explanation</strong> BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]); bSTIterator.next(); // return 3 bSTIterator.next(); // return 7 bSTIterator.hasNext(); // return True bSTIterator.next(); // return 9 bSTIterator.hasNext(); // return True bSTIterator.next(); // return 15 bSTIterator.hasNext(); // return True bSTIterator.next(); // return 20 bSTIterator.hasNext(); // return False</p>



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



<ul><li>The number of nodes in the tree is in the range&nbsp;<code>[1, 10<sup>5</sup>]</code>.</li><li><code>0 &lt;= Node.val &lt;= 10<sup>6</sup></code></li><li>At most&nbsp;<code>10<sup>5</sup></code>&nbsp;calls will be made to&nbsp;<code>hasNext</code>, and&nbsp;<code>next</code>.</li></ul>



<p><strong>Follow up:</strong></p>



<ul><li>Could you implement&nbsp;<code>next()</code>&nbsp;and&nbsp;<code>hasNext()</code>&nbsp;to run in average&nbsp;<code>O(1)</code>&nbsp;time and use&nbsp;<code>O(h)</code>&nbsp;memory, where&nbsp;<code>h</code>&nbsp;is the height of the tree?</li></ul>



<h2><strong>Solution: In-order traversal using a stack</strong></h2>



<p>Use a stack to simulate in-order traversal.</p>



<p>Each next, we walk to the left most (smallest) node and push all the nodes along the path to the stack.</p>



<p>Then pop the top one t as return val, our next node to explore is the right child of t.</p>



<p>Time complexity: amortized O(1) for next() call.<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
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class BSTIterator {
public:
  BSTIterator(TreeNode* root) : root_(root) {}

  int next() {
    while (root_) {
      s_.push(root_);
      root_ = root_-&gt;left;
    }
    TreeNode* t = s_.top(); s_.pop();
    root_ = t-&gt;right;
    return t-&gt;val;
  }

  bool hasNext() {
    return (root_ || !s_.empty());
  }
private:
  TreeNode* root_;
  stack&lt;TreeNode*&gt; s_;
};

/**
 * Your BSTIterator object will be instantiated and called as such:
 * BSTIterator* obj = new BSTIterator(root);
 * int param_1 = obj-&gt;next();
 * bool param_2 = obj-&gt;hasNext();
 */</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-173-binary-search-tree-iterator/">花花酱 LeetCode 173. Binary Search Tree Iterator</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/tree/leetcode-173-binary-search-tree-iterator/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
