struct node { node *left; node *right; int maxleft; int maxright; char value; }; int max=0; void findMaxLen(node *root) { if(root==NULL) return ; if(root->left=NULL) root->maxleft=0; if(root->right==NULL) root->maxright=0; if(root->left!=NULL) findMaxLen(root->left); if(root->right!=NULL) findMaxLen(root->right); //计算左子树最长节点距离 if(root->left!=NULL) { int temp=0; if(root->left->maxleft>root->left->maxright) temp=root->left->maxleft; else { temp=root->left->maxright; } root->left->maxleft=temp+1; } //计算右子树最长节点距离 if(root->right!=NULL) { int temp=0; if(root->right->maxleft>root->right->maxright) temp=root->right->maxleft; else { temp=root->right->maxright; } root->right->maxleft=temp+1; } //更新右子树最长节点的距离 if(root->maxleft+root->maxright>max) max=root->maxleft+root->maxright; }