0

What would be the time and space complexity of a non-binary tree traversal algorithm that prints out every possible path in the tree starting from the root?

1
  • Depends on the traversal. But depth first search would do the job and hence can be done in the order of O(N) where N is number of nodes. Commented Jun 8, 2021 at 16:27

1 Answer 1

1

In case of binary trees, the #edges = #vertices - 1. The traversal takes O(|E|) or O(|V|) time.

In case of non-binary trees, #edges = #vertices - 1 still holds true. So, traversal still takes O(|E|) or O(|V|) time.

Getting every possible path takes same amount of time as traversal = O(|E|) or O(|V|).

If you want to print all the stored paths, it could take O(length of the longest path * number of paths).

Total time complexity =Traversal + Print = O(|E|) + O(length of the longest path * number of paths).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for the detailed explanation!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.