"Leetcode" Binary Search Tree Iterator (2 solutions)

Source: Internet
Author: User

Binary Search Tree Iterator

Implement an iterator over a binary search tree (BST). Your iterator is initialized with the root node of a BST.

Calling would return the next smallest number in the next() BST.

Note: next() hasNext() and should run in average O (1) time and uses O (h) memory, where H is the height of the Tree.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

Solution One:

The sequential output is loaded in the queue after the sequence traversal.

/** Definition for binary tree * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right; * T Reenode (int x): Val (x), left (null), right (NULL) {} *}; */classBstiterator { Public: Queue<int>minq; Map<treenode*,BOOL>m; Stack<treenode *>s; Bstiterator (TreeNode*root) {        //inorder Traversal        if(Root! =NULL)            {S.push (root); M[root]=true;  while(!S.empty ()) {TreeNode* top =S.top (); if(Top->left && m.find (top->left) = =M.end ()) {S.push (top-Left ); M[top->left] =true; Continue; } minq.push (Top-val);                S.pop (); if(Top->right && m.find (top->right) = =M.end ()) {S.push (top-Right ); M[top->right] =true; }            }        }    }    /** @return Whether we have a next smallest number*/    BOOLHasnext () {return!Minq.empty (); }    /** @return The next smallest number*/    intNext () {intFront =Minq.front ();        Minq.pop (); returnFront; }};/** * Your bstiterator'll be called like this: * bstiterator i = bstiterator (root), * while (I.hasnext ()) cout << ; I.next (); */

Solution Two:

Thanks to xcv58, there is no need to advance all the traversal.

Recursive thinking using the middle sequence traversal: When the root node of the subtree is accessed, the subsequent nodes are traversing the right subtree of the root node for the middle sequence.

/** Definition for binary tree * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right; * T Reenode (int x): Val (x), left (null), right (NULL) {} *}; */classBstiterator { Public: Stack<treenode *>s; Bstiterator (TreeNode*root)    {pushleft (root); }    /** @return Whether we have a next smallest number*/    BOOLHasnext () {return!S.empty (); }    /** @return The next smallest number*/    intNext () {TreeNode* top =S.top ();        S.pop (); Pushleft (Top-Right ); returnTop->Val; }        voidPushleft (treenode*root) {        if(Root! =NULL)            {S.push (root); TreeNode* cur =Root;  while(cur->Left ) {S.push (cur-Left ); Cur= cur->Left ; }        }    }};/** * Your bstiterator'll be called like this: * bstiterator i = bstiterator (root), * while (I.hasnext ()) cout << ; I.next (); */

"Leetcode" Binary Search Tree Iterator (2 solutions)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.