binary tree question

From: Sebi (harko_at_cluj.astral.ro)
Date: 02/23/04

  • Next message: Ben Pfaff: "Re: binary tree question"
    Date: 23 Feb 2004 12:24:58 -0800
    
    

    given a binary tree is there any way to find the level that contains
    the highest number of nodes but without using an additional array for
    storing the number of nodes on each level. ?

               1
             / \
            2 3
           / \ / \
          4 5 6 7
          /
         8

    in the example above the third level is the level with the highest
    number of nodes.

    my code is something like:

    int nodes[1000];
    void preOrder(BinaryTreeNode t,int level)
    {
         if (t != null)
         {
            nodes[level]=nodes[level]+1;

            preOrder(t.leftChild,level+1);
            preOrder(t.rightChild,level+1);

            if (nodes[level] > maxlevel)
                   maxlevel=level;
         }
    }

    is there anyway to do this thing without using that array ? maybe by
    using a recursive function ?


  • Next message: Ben Pfaff: "Re: binary tree question"

    Relevant Pages

    • Re: A challenging file to parse
      ... Do the same thing, but when storing in an array, store it at ... Also the data format. ...
      (comp.lang.c)
    • Re: Serializing data vs Storing it in a seperate table
      ... and storing it in a single entry in a table or storing each element of ... Do some research on "database normalization". ... It really depends how big the array is and whether you ...
      (comp.lang.php)
    • Re: A challenging file to parse
      ... algorithm problem and found a Orun-time and Omemory solution, ... Do the same thing, but when storing in an array, store it at ...
      (comp.lang.c)
    • Resizing a structur array in C
      ... doubles but to storing mxArray* pointers. ... I tried to resize a structure array using the C ... not the size of a pointer. ...
      (comp.soft-sys.matlab)
    • Re: an array to a database, am having hard time , help!!
      ... have you tried putting single quotes around the data when you INSERT it? ... Both basically set up a list of acceptable values for the ... As you are storing an array, I would advise you to convert it to a ... an array of the numbers for whatever purpose you wish. ...
      (comp.lang.php)