/** * Definition for binary tree * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int x) { val = x; } * } */ public class BSTIterator { private Queue_values ; public BSTIterator(TreeNode root) { _values = new Queue (); Init(root); } private void Init(TreeNode current) { if(current == null) { return; } Init(current.left); _values.Enqueue(current.val); Init(current.right); } /** @return whether we have a next smallest number */ public bool HasNext() { return _values.Count > 0; } /** @return the next smallest number */ public int Next() { var v = _values.Dequeue(); return v; } } /** * Your BSTIterator will be called like this: * BSTIterator i = new BSTIterator(root); * while (i.HasNext()) v[f()] = i.Next(); */