*binary Search Tree Iterator

Source: Internet
Author: User

title :

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.

Ideas:

To achieve the iterator of a two-fork search tree, find the current minimum value each time. Draw a picture to know that this topic is to examine the binary tree in the middle sequence traversal just.

Without considering the complexity of the space, we can simply write the AC code of the two-fork search tree:

1 import Java.util.ArrayDeque;2 import Java.util.Stack;3 4 5 Public class Bstiterator {6Private arraydeque<treenode>Marraydeque;7     8 Public bstiterator (TreeNode root) {9Marraydeque =NewArraydeque<treenode>();Ten Btreeinordertraverse (root); One     } A      -Privatevoidbtreeinordertraverse (TreeNode root) { -TreeNode p =Root; theStack<treenode> Tmpstack =NewStack<treenode>(); -          -          while(P! =NULL|| !Tmpstack.empty ()) { -             if(P! =NULL) { + Tmpstack.push (p); -p =P.left; +}Else { Ap =Tmpstack.pop (); at Marraydeque.add (p); -p =P.right; -             } -         } -     } -      inPublicBooleanHasnext () { -         return!marraydeque.isempty (); to     } +      -PublicintNext () { the         if(Hasnext ()) { *             returnMarraydeque.poll (). val; $}Else {Panax Notoginseng             return-1; -         } the     } +      A Public Static class TreeNode { the         intVal; + TreeNode left; - TreeNode right; $TreeNode (intx) { $val =x; -         } -     } the}

The space complexity given in the topic is O (h), and the space complexity above is O (2n), which does not meet the requirements of the topic. Therefore, it is necessary to consider how to modify the code logic to solve this problem. The idea is to use the middle sequence traversal of the two-fork tree, but in the case of limited space (PS: only O (h)), we can not do all the middle-order traversal operations in the constructor. Ideas are as follows:
    1. Apply for a stack of only h size.
    2. In the constructor, put all the left children of the tree root node into the stack.
    3. In the next () function, pop the top node of the stack, and if the node has a right child, put the right child and all left children of that right child into the stack.
The idea is very simple, plainly speaking, is to be in the middle sequence traversal algorithm decomposition, in the constructor and next method to complete together. The code is as follows:
1  Public classBstiterator {2     3      Publiclinkedlist<treenode> stack =NewLinkedlist<treenode>();4      Publicbstiterator (TreeNode root)5     {6          while(root!=NULL)7         {8 Stack.push (root);9root=Root.left;Ten         } One          A     } -      -      Public BooleanHasnext () the     { -         return!stack.isempty (); -     } -      +      Public intNext () -     { +TreeNode p=Stack.pop (); A         intresult=P.val; at         if(p.right!=NULL) -         { -TreeNode node =P.right; -              while(Node! =NULL) -             { - Stack.push (node); inNode=Node.left; -             } to         } +         returnresult; -     } the      *}

reference:http://ju.outofmemory.cn/entry/115846

*binary Search Tree Iterator

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.