amzon set 1

amazon set 1

uestion 4: Write a function that returns the length of the longest leaf-to-leaf
path in a binary tree




int longestPath( TreeNode *root, int &maxDistance )


{


if( root == NULL )


{


return -1;


}





int leftHeight = root->left != NULL ? longestPath( root->left,  &maxDistance ) + 1 : 0;


int rightHeigt = roo->right != NULL ? longestPath( root->right, &maxDistance ) + 1: 0;





int distance = leftHeight + rightHeight;


if( distance > maxDistance )


  {


  maxDistance = distance;


  }


 


  return leftHeight > rightHeight ? leftHeight : rightHeight;


}


Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s