Thread: tree maximum level

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    65

    tree maximum level

    Code:
    int Depth(NODE *base)
    {
      if (base==NULL) {
        return(0);
      }
      else {
        int ltree = Depth(base->left);
        int rtree= Depth(base->right);
        if (ltree > rtree) return(ltree+1);
        else return(rtree+1);
      }
    }
    this program finds how many levels the tree has at right and left side and finaly returns the maximum level of the two variables
    my question is after it reach the NULL both int variables will have the 0 value so only the else of the if statement will be TRUE so only the rtree will be increased
    can you please explain me how this code really works

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Look-up how recursive functions work.

    Recursion - Wikipedia, the free encyclopedia

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print all nodes of the same level from binary tree
    By budala in forum C Programming
    Replies: 3
    Last Post: 09-08-2010, 06:31 AM
  2. level of a binary tree
    By BEN10 in forum C Programming
    Replies: 4
    Last Post: 01-09-2009, 12:38 PM
  3. C++ Binary Tree Maximum Value function
    By krizam in forum C++ Programming
    Replies: 4
    Last Post: 12-05-2005, 09:52 AM
  4. Tree [Level Order Treversal]
    By cfrost in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 12:47 AM
  5. Replies: 5
    Last Post: 05-23-2003, 01:11 PM