What is the time complexity of tree traversal?

new299 picture new299 · Feb 10, 2011 · Viewed 35.3k times · Source

What is the time complexity of tree traversal, I'm sure it must be obvious but my poor brain can not work it out right now.

Answer

Uri picture Uri · Feb 10, 2011

It depends what kind of traversal you are performing and the algorithm, but typically it would be O(n) where n is the total number of nodes in the tree. The canonical recursive implementation of depth first traversal, will consume memory (on the stack) in the order of the deepest level, which on a balanced tree it would be log(n).