[FIX] Tree traversal handles branches of different length correctly.

Added tests for these cases.