o find the depth of a binary tree, let us assume that each node of the tree is defined as:
struct Node
{
int data;
Node * left;
Node * right;
};
Now, walk through the left and the right node using recursion to find the depth of the tree
int depth(Node * N)
{
if (N == 0) return -1;
int DL = depth(N->left);
int DR = depth(N->right);
return (DL > DR) ? DL+1:DR+1;
}
struct Node
{
int data;
Node * left;
Node * right;
};
Now, walk through the left and the right node using recursion to find the depth of the tree
int depth(Node * N)
{
if (N == 0) return -1;
int DL = depth(N->left);
int DR = depth(N->right);
return (DL > DR) ? DL+1:DR+1;
}
Comments
Post a Comment
https://gengwg.blogspot.com/