问题
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.
回答1:
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).
回答2:
Wouldn't that just be n for a tree with n nodes?
You visit each tree-leave once, don't you? So i'd say it is linear.
来源:https://stackoverflow.com/questions/4956347/what-is-the-time-complexity-of-tree-traversal