My C# Super Easy Understanding Solution


  • 0
    Y

    public class BSTIterator
    {

        List<int> theList = new List<int>();
    
        public BSTIterator(TreeNode root)
        {
            Go(root);
        }
    
        void Go(TreeNode root)
        {
            if (root == null)
                return;
            Go(root.left);
            theList.Add(root.val);
            Go(root.right);
        }
        /** @return whether we have a next smallest number */
        public bool HasNext()
        {
            return theList.Count > 0;
        }
    
        /** @return the next smallest number */
        public int Next()
        {
            var temp = theList[0];
            theList.RemoveAt(0);
            return temp;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.