二叉树相隔最远的俩点距离

来源:互联网 发布:数据库管理专业 编辑:程序博客网 时间:2024/06/05 05:02
/*struct TreeNode {    int val;    struct TreeNode *left;    struct TreeNode *right;    TreeNode(int x) :            val(x), left(NULL), right(NULL) {    }};*/class LongestDistance {public:    int findLongest(TreeNode* root) {        if(root==NULL){            return 0;        }        int left_max=findLongest(root->left);        int right_max=findLongest(root->right);        int l=get_len(root->left);        int r=get_len(root->right);        return max(l+r+1,max(left_max,right_max));        // write code here    }    int get_len(TreeNode *root){        if(root==NULL){            return 0;        }        return max(get_len(root->left),get_len(root->right))+1;    }};

0 0