I am looking a lot of interview problems where the question requires traversing two trees together, I am not sure exactly how to go about doing this.
e.g.
Given references to roots of two binary trees, how do you determine
whether the sequence of the leaf elements equal but you must
implement short circuiting return when the first node violates the
rule.
Is your question asking to find out whether:
“the sequence created by visiting all the leaf nodes of 2 trees is same”
with a constraint that when found a mismatch of leaf values, then we must quit immediately.
If so, I propose following solution: