求二叉树高度用++永远返回0

练习数据结构的时候发现求二叉树高度的时候用L++和R++,返回的数值是0,而用L+1和R+1能返回正确的数值,这是为什么呢

int TreeDepth(TreeNode* pRoot) {
    if (pRoot == NULL) {
        return 0;
    }
    int L = TreeDepth(pRoot->lchild);
    int R = TreeDepth(pRoot->rchild);
    return L > R ? L+1 : R+1;  //能正确返回高度
}

int TreeDepth(TreeNode* pRoot) {
    if (pRoot == NULL) {
        return 0;
    }
    int L = TreeDepth(pRoot->lchild);
    int R = TreeDepth(pRoot->rchild);
    return L > R ? L+1 : R+1;  //不能正确返回高度
//注意不能用 return L > R ? L++ : R++;原因暂时不明
}

注意 L++ 这种是后自增运算符,会先返回 L 的值之后,再把 L 的值加一,因此如果用第二个写法,某种程度上等价于:

int TreeDepth(TreeNode* pRoot) {
    if (pRoot == NULL) {
        return 0;
    }
    int L = TreeDepth(pRoot->lchild);
    int R = TreeDepth(pRoot->rchild);
    return L > R ? L : R;  //不能正确返回高度
    L += 1;
    R += 1;
}

显然,能看到 +1 的操作在 return 之后,那么相当于没有 +1,结果也就是 0 了。