title: Find the maximum element per level in a BST
body: <p>Find the maximum element per level in a BST .? </p>
  
  <pre><code>[1] In O(n) time and O(1) space
  [2] In O(logn) time and O(n) space
  </code></pre>
  
  <p>Edit: The solution posted by @Imposter works fine for [1]
  Here is the solution for [1] </p>
  
  <pre><code>private int level = 0;
  private int VisitedLevels = -1;
  
  public void findLargestByLevel(AvlNode root)
  {
      if(root == null) return;
  
      else
      {
          if(level &gt; VisitedLevels)
          {
              System.out.println(root.data + " @ Level = " + level);
              VisitedLevels++;
          }
          level++;
  
          findLargestByLevel(root.right);
          findLargestByLevel(root.left);
  
          level--;
      }
  }
  </code></pre>
  
  <p>but I am still not able to work out a solution for [2]</p>
  
  <p>The approaches that I have thought : If we preprocess the tree and flatten it like serialization of the tree,</p>
  
  <pre><code>                 100
               50         200
           20      75
  
  #L0, 100, #L1, 50, 200, #L2, 20, 75, #L3
  </code></pre>
  
  <p>Where the #L is a marker for the levels:</p>
  
  <p>then we can easily answer the queries for the level's highest and lowest in O(1) time, 
  Also if the tree get modified we can perform the insertion and deletion from the serialized data in LogN time.
  Please suggest someone for the [2], though in my opinion zit looks impossible to achieve [2] but I would like hear other's suggestions</p>