Advertisement
ogv

Untitled

ogv
Sep 10th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. // Runtime: 59 ms, faster than 32.73% of Java online submissions for Binary Search Tree Iterator.
  2. // Memory Usage: 51 MB, less than 90.12% of Java online submissions for Binary Search Tree Iterator.
  3. class BSTIterator {
  4.     LinkedList<Step> steps;
  5.    
  6.     public BSTIterator(TreeNode root) {        
  7.         steps = new LinkedList<Step>();
  8.         if (root != null){
  9.             steps.add(new Step(root));
  10.         }
  11.     }
  12.    
  13.     /** @return the next smallest number */
  14.     public int next() {
  15.         move();
  16.         int result = steps.getFirst().node.val;
  17.         return result;
  18.     }
  19.    
  20.     private void move() {        
  21.         if (steps.isEmpty()) return;
  22.        
  23.         for (;;) {
  24.             Step step = steps.getFirst();
  25.             if (step.visitedLeft == false) {                
  26.                 step.visitedLeft = true;
  27.                 if (step.node.left != null) {                
  28.                     steps.addFirst(new Step(step.node.left));
  29.                     continue;
  30.                 }            
  31.             }
  32.  
  33.             if (!step.returned) {
  34.                 step.returned = true;                
  35.                 break;
  36.             }
  37.  
  38.             if (step.visitedRight == false) {                
  39.                 step.visitedRight = true;
  40.                 if (step.node.right != null) {
  41.                     steps.addFirst(new Step(step.node.right));
  42.                     continue;
  43.                 }            
  44.             }
  45.            
  46.             steps.removeFirst();            
  47.         }
  48.     }
  49.    
  50.     /** @return whether we have a next smallest number */
  51.     public boolean hasNext() {        
  52.         for (int i = 0; i < steps.size(); i++) {
  53.             Step step = steps.get(i);
  54.          
  55.             if (!step.visitedLeft && step.node.left != null) {
  56.                 return true;
  57.             }            
  58.             if (!step.visitedRight && step.node.right != null) {                
  59.                 return true;
  60.             }            
  61.             if (!step.returned) {
  62.                 return true;
  63.             }            
  64.         }        
  65.         return false;
  66.     }
  67.    
  68.     class Step {
  69.         TreeNode node;
  70.         boolean visitedLeft;
  71.         boolean visitedRight;
  72.         boolean returned;
  73.         public Step(TreeNode node ) {
  74.             this.node = node;
  75.         }
  76.     }
  77. }
  78.  
  79. /**
  80.  * Your BSTIterator object will be instantiated and called as such:
  81.  * BSTIterator obj = new BSTIterator(root);
  82.  * int param_1 = obj.next();
  83.  * boolean param_2 = obj.hasNext();
  84.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement