------
## 最终结论
```python
def totalFruit(tree):
    if not tree:
        return 0

    last_fruit, second_last_fruit = -1, -1
    last_fruit_count, current_max, max_fruit = 0, 0, 0

    for fruit in tree:
        if fruit == last_fruit or fruit == second_last_fruit:
            current_max += 1
        else:
            current_max = last_fruit_count + 1
        
        if fruit == last_fruit:
            last_fruit_count += 1
        else:
            last_fruit_count = 1
            second_last_fruit = last_fruit
            last_fruit = fruit
        
        max_fruit = max(max_fruit, current_max)

    return max_fruit

# Example usage:
print(totalFruit([1,2,1])) # Output: 3
print(totalFruit([0,1,2,2])) # Output: 3
print(totalFruit([1,2,3,2,2])) # Output: 4
print(totalFruit([3,3,3,1,2,1,1,2,3,3,4])) # Output: 5
```