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

  • Home
  • SEARCH
  • 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 9226091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:44:44+00:00 2026-06-18T04:44:44+00:00

Given a preorder traversal of a full binary tree, where each node is labeled

  • 0

Given a preorder traversal of a full binary tree, where each node is labeled either a leaf node or an internal node, is there a good algorithm to find the height of the tree? For example, if N represents an internal node and L represents a leaf, then given the preorder traverseal NLNNLLL, the height would be three.

  • 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-18T04:44:45+00:00Added an answer on June 18, 2026 at 4:44 am

    Alright, I can’t help but feel bad that we’re leaving marti hanging in the comments. I think he truly doesn’t know where to start, and has at least demonstrated that he’s thought about the problem.

    What do we know about a full binary tree? Each node is either a leaf or has two children.

    A preorder traversal recursively visits the root, left subtree, then right subtree.

    Think about this question: at what point in the preorder traversal (of a full binary tree) do we know we’ve exhausted a subtree? We will have visited its root, and then two leaves (or just the root if it’s a leaf).

    Let’s make a stack of a special structure:

    struct StackNode{
       size_t count; //initialize to 0
       char nodeType; //'N' or 'L'
    };
    

    This ‘StackNode’ object will track what type of node we visited in our preorder traversal using the ‘nodeType’ variable, which should be clear. We also have a special counter ‘count’ which we initialize to 0.

    The idea behind a solution would be this:

    • each time you encounter a ‘N’, create a StackNode, and push it onto the stack.
    • each time you encounter a ‘L’, create a StackNode, and push it onto the stack
    • if the last node you pushed onto the stack was ‘L’, then pop the last node off, and then increment stack.top()’s count by 1
    • if stack.top()’s count is 2, then pop the top off the stack, and then increment stack.top()’s count by 1 (repeat until stack is empty or you’ve stopped popping off the stack)

    Every time you push a node onto the stack, you can check the current height of your tree. It is the number of items in your stack-1 (accounting for the item on the bottom being the root).

    As long as your track the maximum height you’ve encountered thus far, you will find the height of your tree.

    Let’s work through your example: NLNNLLL


    Stack is initially empty.

    int maxHeight = -1;
    

    process first character: N

    push a node onto the stack:

    Stack: Type Count

    • N, 0

      maxHeight = 0;


    process next character: L

    push a node onto the stack:

    • L, 0
    • N, 0

      maxHeight = 1; //(incremented by 1)

    The last character processed was a leaf, so pop and increment:

    stack:

    • N, 1

      maxHeight = 1;


    process next character: N

    push a node onto the stack:

    • N, 0
    • N, 1

      maxHeight = 1; //unchanged


    process next character: N

    push a node onto the stack:

    • N, 0
    • N, 0
    • N, 1

      maxHeight = 2; //(incremented by 1)


    process next character: L

    push a node onto the stack:

    • L, 0
    • N, 0
    • N, 0
    • N, 1

      maxHeight = 3; //incremented by 1

    last node was a leaf, so pop and increment

    stack:

    • N, 1
    • N, 0
    • N, 1

      maxHeight = 3; //unchanged


    process next character: L

    push a node onto the stack:

    • L, 0
    • N, 1
    • N, 0
    • N, 1

      maxHeight = 3; //unchanged

    last node was a leaf, so pop and increment:

    • N, 2
    • N, 0
    • N, 1

    top node has count 2, so pop and increment:

    • N, 1
    • N, 1

    process next node: L

    push a node onto the stack:

    • L, 0
    • N, 1
    • N, 1

      maxHeight = 3; //unchanged

    last node was a leaf, so pop and increment:

    • N, 2
    • N, 1

    top node has count 2, so pop and increment:

    • N, 2

    top node has count 2, so pop and increment:

    (empty stack), finished
    
    maxHeight = 3; //the maximum height discovered during a preorder of a full binary tree
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to construct a Binary Search Tree Given only its preorder traversal
I've given only a pre-order traversal sequence of a binary tree (e.g. {a, b,
I have just started studying Binary Tree. Is there an algorithm to find out
I know you can reconstruct a binary tree when given its inorder and preorder
I am implementing a tree which is a Binary Expression Tree. The leaf nodes
I'm trying to construct a binary tree (unbalanced), given its traversals. I'm currently doing
I need to build a binary tree from a preorder bitstring (which is piped
Given a tree, how to find the centre node in the tree so that
Given the jQuery dropdown plugin below. Is there a way to add a method
I'm trying to write binary search tree's content to temporary array in order to

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.