Finding the Depth of a Binary Tree

Hello, as the title states, I am trying to find the depth of an already created Binary Tree using the depth function below. I keep getting an error "invalid use of incomplete type struct node." Any help? Thanks!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````#include #include #include #include #include using namespace std; struct BinaryTree { int item; BinaryTree *left; BinaryTree *right; }; BinaryTree * newNode(int value){ BinaryTree *node=new BinaryTree; node->item=value; node->left=NULL; node->right=NULL; return (node); } int maxDepth(struct node* node) { if (node==NULL) { return(-1); } else { // compute the depth of each subtree int LDepth = maxDepth (node->left); int RDepth = maxDepth (node->right); // use the larger one if (LDepth > RDepth) return(LDepth +1); else return(RDepth +1); } } int main( ){ struct BinaryTree *root; int temp = 0; root=newNode(3); root->left=newNode(1); root->right=newNode(5); root->left->right=newNode(2); root->right->left=newNode(4); root->right->right=newNode(6); temp = maxDepth(root); } ``````
Line 26: Change `struct node` to `BinaryTree`.
Shouldn't line 28 return 0 instead of -1? Right now a single node with no children will return a depth of 0 instead of 1.

Lines 35-39 might be clearer as
`return 1+max(LDepth, RDepth);`
Topic archived. No new replies allowed.