I have to create an algorithm to find the longest path of even value in a binary tree without using recursion. For example, if my tree is the following: Binary Tree
The algorithm should return 3
as that is the length of the longest path of only even value.
Requirements:
- We have two queues we can use
- The path must start from the root(which we are given)
What I have in mind as of now:
- Use
Morris Traversal
to go over the tree, but I don't know in which part of the code to do the "path search" portion - Somehow, use the queues to save the relevant nodes,
q1
would represent thetempLongestPath
and another one that isoverallLongestPath.
But I don't have a clear idea about saving the path's length while going over the tree.
Thanks in advance for the help!