What is the time complexity of tree traversal?
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.
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).
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.
精彩评论