Binary search tree iterator always show Runtime Error, but my local UT can pass, Why?


  • 0
    D

    https://leetcode.com/problems/binary-search-tree-iterator/

    Below is my code, I am sure it is correct, but OJ always show:

    Submission Result: Runtime Error More Details
    Last executed input: {1}

    Anybody can give some suggestions?

    public class BSTIterator
    {
    	readonly Stack<TreeNode> _mystack = new Stack<TreeNode>(); 
    	public BSTIterator(TreeNode root)
    	{
    		findMostLeftNode(root);
    	}
    
    	/** @return whether we have a next smallest number */
    	public bool HasNext()
    	{
    		return _mystack.Count > 0;
    	}
    
    	/** @return the next smallest number */
    	public int Next()
    	{
    		var top = _mystack.Pop();
    		findMostLeftNode(top.right);
    
    		return top.val;
    	}
    
    	private void findMostLeftNode(TreeNode root)
    	{
    		while (root != null)
    		{
    			_mystack.Push(root);
    			root = root.left;
    		}
    	}
    }

Log in to reply
 

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