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.
Given a Binary Tree, the task is to print the elements in post order using O(N) time complexity and constant space.
c# - Fastest tree traversal - Stack Overflow.
6. What is the time complexity of level order traversal? Explanation: Since you have to go through all the nodes, the complexity becomes O(n).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With