【leetcode】二叉树的直径

 

int recursion(struct TreeNode* root, int* max){
    if (!root) 
        return 0;
    int left=recursion(root->left,max);
    int right=recursion(root->right,max);
    if(left+right>*max) 
        *max=left+right;
    return (left>right)?left+1 :right+1;
}
int diameterOfBinaryTree(struct TreeNode* root){
    int max=0;
    recursion(root,&max);
    return max;
}

 

posted @ 2020-11-20 15:06  温暖了寂寞  阅读(74)  评论(0编辑  收藏  举报