Facebook | Onsite | 🌳 Find first pair of mismatching nodes
Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2019-07-13Added By Anonymous
Source:Â Link
Company: Meta (Facebook)
Role: unknown
Interview Stage: Onsite
Location: unknown
Year: unknown
Original Post Content:
Return first pair of mismatching nodes (first pair as in in-order) given two pre-order traversal arrays of BSTs.Example 1:
Input: pre1 = [5, 4, 2, 4, 8, 6, 9], pre2 = [5, 3, 2, 4, 8, 7, 9] Output: [4, 3] Explanation: Tree 1: 5 4 8 2 4 6 9 Tree 2: 5 3 8 2 4 7 9 inorder1 = [2, 4, 4, 5, 6, 8, 9] inorder2 = [2, 3, 4, 5, 7, 8, 9]
Example 2:
Input: pre1 = [2, 1, 3], pre2 = [1, 2] Output: [3, null] Explanation: Tree 1: 2 1 3 Tree 2: 1 2 inorder1 = [1, 2, 3] inorder2 = [1, 2]
Example 3:
Input: pre1 = [2, 1, 3], pre2 = [1, 2, 3] Output: [] Explanation: Tree 1: 2 1 3 Tree 2: 1 2 3 inorder1 = [1, 2, 3] inorder2 = [1, 2, 3] There is no mismatch because the in-order sequence for both is exactly the SAME, despite the trees are structurally different.