Is the runtime of BFS and DFS on a binary tree O(N)?

无人久伴 提交于 2019-12-03 06:02:13

The time complexities for BFS and DFS are just O(|E|), or in your case, O(m).

In a binary tree, m is equal to n-1 so the time complexity is equivalent to O(|V|). m refers to the total number of edges, not the average number of adjacent edges per vertex.

Yes, O(n) is correct.

Also note that the number of edges can more exactly be expressed as the number of nodes - 1. This can quite easily be seen by considering that each node, except the root, has an edge from its parent to itself, and these edges cover all edges that exists in the tree.

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