Dsa
Trees
Easy
Leaf Similar Trees
Given two binary trees, determine if their leaf sequences are similar. A leaf sequence is defined as the sequence of leaf nodes read from left to right.
Input format:
- The first line contains an integer N, the number of nodes in the first tree.
- The next N lines each contain one integer that represents the value of the nodes in the first tree in level-order traversal. Use -1 to indicate 'null' nodes.
- The following line contains an integer M, the number of nodes in the second tree.
- The next M lines each contain one integer that represents the value of the nodes in the second tree in level-order traversal. Use -1 to indicate 'null' nodes.
Output format:
- Print 'True' if the leaf sequences are similar, otherwise print 'False'.
Example:
Input:
5
3
5
1
2
-1
4
7
2
1
-1
-1
Output:
False
Key concepts
treesrecursioncomparison
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now