## Given a binary tree, return the length of the longest path. Length is defined as number of nodes in the path

struct node { struct node* left; struct node* right; int data; }; typedef struct node TreeNode; int longestPath(TreeNode* current, int *height) { if(current==NULL) { *height = 0; return 0; } else { int lh=0,rh=0; int lp_left = longestPath(current->left, &lh); int lp_right = longestPath(current->right, &rh); *height = lh+rh+1; return max(*height,max(lp_left,lp_right)); } }

Advertisements