题目:寻找两个视图所有的公共的父视图。

- (void)p_findCommonSuperView {
    UIView *a = [UIView new];
    UIView *b = [UIView new];
    UIView *c = [UIView new];
    UIView *d = [UIView new];
    UIView *e = [UIView new];
    UIView *f = [UIView new];
    [self.view addSubview:a];
    [a addSubview:b];
    [b addSubview:c];//a - > b -> c
    [b addSubview:d];//a - > b -> d -> e -> f
    [d addSubview:e];
    [e addSubview:f];
    NSArray *arr = [self findCommonSuperForViewA:c viewB:f];//3个 self.view -> a -> b
    NSLog(@"%ld", arr.count);
}

- (NSArray<UIView *> *)findCommonSuperForViewA:(UIView *)a viewB:(UIView *)b {
    NSMutableArray *resultM = [NSMutableArray array];
    NSArray <UIView *>*arrA = [self findAllSuperViews:a];
    NSArray <UIView *>*arrB = [self findAllSuperViews:b];
    //共同父视图最少0个,最多 MIN(arrA.count, arrB.count)
    NSInteger maxCount = MIN(arrA.count, arrB.count);
    int i = 0;
    while (i < maxCount) {
        UIView *theA = [arrA objectAtIndex:(arrA.count - 1 - i)];
        UIView *theB = [arrB objectAtIndex:(arrB.count - 1 - i)];
        if (theA == theB) {
            [resultM addObject:theA];
            i++;
        } else {
            break;
        }
    }
    return resultM;
}

- (NSArray <UIView *>*)findAllSuperViews:(UIView *)v {
    NSMutableArray *arrM = [NSMutableArray array];
    UIView *s = v.superview;
    while (s) {
        [arrM addObject:s];
        s = s.superview;
    }
    return arrM.copy;
}

- (NSArray<UIView *> *)findCommonSuper2ForViewA:(UIView *)a viewB:(UIView *)b {
    NSMutableArray *resultM = [NSMutableArray array];
    NSInteger aCount = [self viewCountForView:a];
    NSInteger bCount = [self viewCountForView:b];
    UIView *longView, *shortView;
    NSInteger delta;
    if (aCount > bCount) {
        longView = a;
        shortView = b;
        delta = aCount - bCount;
    } else {
        longView = b;
        shortView = a;
        delta = bCount - aCount;
    }
    while (delta > 0) {
        longView = longView.superview;
        delta -= 1;
    }
    while (longView && shortView) {
        if (longView == shortView) {
            [resultM addObject:longView];
        }
        longView = longView.superview;
        shortView = shortView.superview;
    }
    return resultM;
}