pygame – Layer sprites according to their vertical position

I need advanced blitting options in my game. It’s a 2D game that has trees’ sprites along with the player’s sprite.

I want that every tree sprite to be displayed either in front of or behind the player sprite, depending on whether the player is standing higher or lower on the screen than the tree’s base.

These are the possible cases :

cases

I have an idea in mind, which is to check for the player’s coordinates and depending on them, blit the tree’s sprite underneath it or above it (respecting the layers system). However, I think that this won’t work or will make the tree’s sprite flicker in case there is more than one player (or another object that can pass beside the tree). So what is a good solution for that ?

The reason why I want to solve this problem is adding some depth and reality into the game though it’s a 2D one.