There are a few things to consider before locking your bike to a tree. The first is the health of the tree. Locking your bike to a tree can damage the bark and potentially kill the tree. If you must lock your bike to a tree, try to avoid doing so directly around the trunk and use a soft material like a cloth or rubber tube as added protection.
The second consideration is the stability of the tree. A healthy, mature tree is much more likely to be able to withstand the weight and movement of a locked bike than a young or sickly one. Dead trees should obviously be avoided entirely.
Finally, you'll want to make sure that there's nothing else nearby that your bike could be securely locked to instead. Street signs, parking meters, and even other bikes are all better options than trees. In general, it's best to err on the side of caution when it comes to locking up your bike - after all, it's usually your only means of transportation!