Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Trying to find depth of binary tree

binary tree c++

This topic has been archived. This means that you cannot reply to this topic.
No replies to this topic

#1 sonar87

sonar87

    CC Newcomer

  • Member
  • PipPip
  • 20 posts

Posted 12 April 2017 - 03:05 PM

I'm trying to write something to determine the largest depth of a binary tree but far have only ended up with one thing that keep giving back the number of nodes in the tree, and another, below, that is always one more or less off. After hours of trying to adjust this I could really use some advice..

void findthedepth(nodeoftree<node>* root, int* depthtotal, int* depthcurrent){

        int left = 0, right = 0;

       if( root == nullptr ){
           *depthtotal = 0;
           *depthcurrent = 0;
           return;
       }

       findthedepth(root->rightp(), depthtotal, depthcurrent);
       right = *depthcurrent;

       *depthcurrent = 0;

       findthedepth(root->leftp(), depthtotal, depthcurrent);
       left = *depthcurrent;


       if (left > right){ 
           *depthtotal += left + 1;
       }
       else { 
           *depthtotal += right + 1;
       }


   }