Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8564349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:10:48+00:00 2026-06-11T17:10:48+00:00

Find the maximum element per level in a BST .? [1] In O(n) time

  • 0

Find the maximum element per level in a BST .?

[1] In O(n) time and O(1) space
[2] In O(logn) time and O(n) space

Edit: The solution posted by @Imposter works fine for [1]
Here is the solution for [1]

private int level = 0;
private int VisitedLevels = -1;

public void findLargestByLevel(AvlNode root)
{
    if(root == null) return;

    else
    {
        if(level > VisitedLevels)
        {
            System.out.println(root.data + " @ Level = " + level);
            VisitedLevels++;
        }
        level++;

        findLargestByLevel(root.right);
        findLargestByLevel(root.left);

        level--;
    }
}

but I am still not able to work out a solution for [2]

The approaches that I have thought : If we preprocess the tree and flatten it like serialization of the tree,

                 100
             50         200
         20      75

#L0, 100, #L1, 50, 200, #L2, 20, 75, #L3

Where the #L is a marker for the levels:

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:10:49+00:00Added an answer on June 11, 2026 at 5:10 pm

    If the BST is Full BST then it can be done in log(N) time because all you need to do is traverse towards right all the time (since elements on right side are always grater than left side.) If it is not full BST then we have to traverse all the elements because we are not sure that height of right sub tree is always greater than left sub tree.

    Example : If right sub tree has two level and left sub tree has three levels then using above approach we are able to print max value till two level but we missed out third level which is not present in right sub tree .

    So time complexity will be minimum O(n) if it is not Full BST and may be more if no extra space is given .

    If you do BFS it takes only O(n) time complexity and O(n) space complexity . If you want it using DFS then following algorithm would help you in O(n) time complexity and and O(h) where h is height of tree .

    Take global variable counter which indicates max number of levels so far while traversing .

    Take two variables L and R when ever you make recursive call to left sub tree increment L

    similarly when ever you make recursive call to right sub-tree increment R .

    Find maximum of L and R for each node which gives level number .

    When ever there is increment in max(L,R) for a node while traversing check this with counter if counter is less than max(L,R) then allocate memory and intialize to zero and increment counter.(That means we are actually creating variable for each level in tree).

    while traversing we will check for height or level variable every time and compare it with present node that is being considered if present node is bigger than Level variable then update level variable with node that is being considered .

    After traversal print height or level variables .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Earlier today I asked similar problem to find the maximum element which is common
Given two arrays, how to find the maximum element which is common to both
I need to find the maximum route of a triangle. I earlier posted this
Given a binary search tree (BST). Find the maximum depth of the binary search
How can I find the maximum element and its index from an array in
This code is about to find maximum element from an array i want to
I need to find out the maximum and minimum value in a line by
Is it possible to find the second maximum number from an array of integers
Where can I find what is the maximum identifier length in C? In which
How to find the integer occurring maximum number of times (mode) in an unsorted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.