What is the time complexity of tree traversal?

三世轮回 提交于 2019-12-03 11:07:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!