【leetcode】-69-二叉树的直径

由求二叉树深度的递归算法改进而来 https://blog.csdn.net/qq_39328436/article/details/113729608

最长直径其实就是  左边深度+右边深度

 int res=0;
    int height(TreeNode* root){

        if(root==NULL) return 0;
        int left=height(root->left); 
        int right=height(root->right);

        res=max(res,left+right); //只有这一句是加的,其他和递归求节点深度一模一样

        return max(left,right)+1;//返回深度
    }

    int diameterOfBinaryTree(TreeNode* root) 
    {
       height(root);
       return res;   
    }

猜你喜欢

转载自blog.csdn.net/qq_39328436/article/details/113729519
今日推荐