Merge Two Binary Trees
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
You are given two binary trees, root1 and root2. Overlay one on top of the other to produce a single merged tree and return its root.
At each position: if both trees have a node, the merged node's value is the sum of the two values. If only one tree has a node there, that node (with everything beneath it) is used as is. If neither does, the merged tree has no node there.
Merging starts at the two roots. You may reuse the input nodes or allocate new ones; the result is graded in level-order form.
Examples
Constraints
- 0 <= number of nodes in each tree <= 2000
- -10^4 <= Node.val <= 10^4
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.