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 6934379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:57:08+00:00 2026-05-27T11:57:08+00:00

public void HeightIterative() { int counter = 0; int counter2 = 0; TreeNode current=root;

  • 0
  public void HeightIterative()
    {
        int counter = 0;
        int counter2 = 0;
        TreeNode current=root;

        if(current != null)
        {
            while(current.LeftNode!=null)
            {
                counter++;
                current = current.LeftNode;
            }
            while(current.RightNode!=null)
            {
                counter2++;
                current = current.RightNode;
            }
        }

        int res = 1+Math.Max(counter, counter2);
        Console.WriteLine("The Height Of Tree Is: "+res);
    }

I wrote iterative method, to calculate height of tree. but in some cases its not working properly. As in case:
10
1
2
3
4
5
18
17
16
15
14
13
what’s the problem. according to this sequence height of tree is 6 where as my code is showing 5.

  • 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-05-27T11:57:08+00:00Added an answer on May 27, 2026 at 11:57 am

    You are using two loops, but each loop investigated only oneside of node, but each node in tree has two sides you should investigate it all. You can do it through recursion call.

    private int GetLen(TreeNode node)
    {
      var result = 0;
    
      if(node != null)
      {
        result = Math.Max(GetLen(node.LeftNode), GetLen(node.RightNode)) + 1;
      }
    
      return result;
    }
    
    public void HeightIterative()
    {
      int res = GetLen(root);
      Console.WriteLine("The Height Of Tree Is: "+res);
    }
    

    Iterative version:

    private class NodeInfo
    {
      public NodeInfo(TreeNode node, int len)
      {
        Node = node;
        Len = len;
      }
    
      public TreeNode Node {get; private set;}
      public int Len {get; private set;}
    }
    
    public void HeightIterative()
    {
        int maxLen = 0;
    
        var queue = new Queue<NodeInfo>();
        queue.Enqueue(new NodeInfo(root, 1));
    
        while (queue.Count > 0)
        {
            var item = queue.Dequeue();
            var current = item.Node;
            var currentLen = item.Len;
    
            if (current.LeftNode != null)
            {
                queue.Enqueue(new NodeInfo(current.LeftNode, currentLen + 1));
            }
    
            if (current.RightNode != null)
            {
                queue.Enqueue(new NodeInfo(current.RightNode, currentLen + 1));
            }
    
            if (currentLen > maxLen)
            {
                maxLen = currentLen;
            }
        }
    
        Console.WriteLine("The Height Of Tree Is: " + maxLen);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public void iterativePreorder(Node root) { Stack nodes = new Stack(); nodes.push(root); Node currentNode; while
public void test() { List<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3); for (int
public void ScoreFirstBall(int pinsKnockedDown) { if (IsStrike(Frame, pinsKnockedDown)) { Score = X; ScoreMessage =
public void onListItemClick(ListView parent, View v, int position, long id) { super.onListItemClick(parent, v, position,
public void testNullsInName() { fail(sample failure); Person p = new Person(null, lastName); assertEquals(lastName, p.getFullName());
public void Stream(String FOLDER_PATH, int port){ File myDir = new File(FOLDER_PATH); File[] files =
this: public void foo() { for (int i = 0; i < rows; i++)
public void handleParsedCommand(String[] commandArr) { if(commandArr[0].equalsIgnoreCase(message)) { int target = Integer.parseInt(commandArr[1]); String message =
public void displayData(String [] names, int []scores, char [] grades){ for (String name :
public void Find() { String Value = ; System.out.println(Search Name); Value = Input.next(); int

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.