开发者

What is the time complexity of tree traversal?

开发者 https://www.devze.com 2023-02-09 22:23 出处:网络
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 th

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消