#include <iostream> using namespace std; typedef struct tagNode { char data; tagNode *lchild; tagNode *rchild; }*PNode; void createtree(PNode &T) { char c; cin >> c; if(c == ',')return; else { T = new tagNode; T->data = c; T->lchild = NULL; T->rchild = NULL; createtree(T->lchild); createtree(T->rchild); } } int helper(const PNode T, const PNode root) { if( T == NULL)return 0; int ldepth = helper(T->lchild,root); int rdepth = helper(T->rchild,root); if( T == root) return ldepth+rdepth+1; else return max(ldepth,rdepth)+1; } int TreeMaxDistance(PNode T) { helper(T,T); } int main() { PNode T = NULL; createtree(T); if( T == NULL)return 0; cout << TreeMaxDistance(T) << endl;; }
时间: 2024-11-05 23:37:07