Sometimes I have seen some code or part of the project which I could improve but is not related with my current team project.
Those times I have a conflict because despite wanting to help, many teams lack enough people and doing extra work seems like betrayal. Obviously any managers will appreciate much more if you focus your effort on their tasks
What do you do in in these cases?
I think the key part of your statement is this:
which I could improve but is not related with my current team project
This sounds harsh, but it is based on experience and should be read with an open mind - keep out of it. One thing you learn as you get more dev experience under your belt is that you stay in scope, don't go diving off to fix something that is not scheduled. Here are a couple of reasons why:
when someone is planning the project, they are working with a known set of problems. If you randomly decide to "fix" things you are changing that known set, it is like when you are navigating using a map and someone is changing the map on you
you may think your fix is good for the product but it introduces extra complexities, like who is going to test your fix?
no matter how good you think you are, you run the risk of introducing another bug or unintended side effects into the application
how do you communicate your change to the other team members who may be working in that area?
how do you know for sure the scope of your change, how do you know that you haven't suddenly changed the context of a piece of code elsewhere?
You have good intentions, but if you start doing things like this you may be seen as a cowboy, and others will dislike you pretty quickly.
It is a skill you have to learn - know when not to touch things. You will see ugly code - don't touch! Don't recode things simply because you think it can be done better.
I hope this helps :)