When you're traversing a tree, you're visiting every node just once probably and logically time complexity of it, is O(n).But what if you're traversing using nested loops (2 , 3 nested for loops for example) why time complexity isn't O(n^2) or O(n^3)?I sense that it could be about bounds but,can't be sure due to lack of knowledge.I would really appreciate if someone explains this cleanly.
edit: sorry, I don't have an example code to show.As said in the answer below, I dont know actually too (nor I could find any example code.) , how could one traverse using multiple nested loops.It was just an what if question occured in my mind when I was thinking about that.But the below answer pretty much satisfies my confusion.
The complexity of your code has nothing to do with the number of loops you use. It depends on what exactly these loops are doing.
If you are using 2/3/4/n loops in your tree traversal code in such a way that you visit every node in the tree only once, then your complexity is still O(n), though I'm not sure how in this specific example you could traverse a tree once using multiple nested loops.
If however, you have 2 loops, such that for every iteration of the first loop (every node of the tree), your 2nd inner loop performs another full traversal of the tree, then your complexity would be O(n^2).