Thursday, February 16, 2012

Sequences of the leaf elements of two binary trees are same or not

Given references to roots of two binary trees, how do you short circuit determine whether the sequences of the leaf elements of both the trees are same ? The structure of two BTs may be different. Short circuit : for ex. If the very first leaf element of each tree is different, the algorithm should stop immediately returning false instead of checking all the leaf elements of both trees.

                                           5
                               /                       \
                         2                               7
                                       5
                                          \
                                           3
                                      /           \
                                    2             7
for both the above this should return true
but with below one it is false
                                          5
                                        /       \
                                       3        7

No comments:

Post a Comment