FastPrepCheck Identical Binary Trees
Problems/Check Identical Binary Trees

Check Identical Binary Trees

Apple logoApple● EasyNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Apple new grad onsite interview. The task compares the shape and values of two trees, including the trees in the pictures. This part is about 98% the same as the report. We also chose what happens for empty trees, that the input must not be changed, and the input limits.

Return whether two binary trees are identical: they must have the same structure and the same value at every corresponding node. A missing left child must match a missing left child, and likewise for right children.

Do not modify either tree. Two empty trees are identical. Trees are displayed in level order with null for missing children.

Function

sameTree(first: TreeNode, second: TreeNode) → boolean

Examples

Example 1

first = [5,2,3,null,null,6]second = [5,2,3,null,null,6]return = true
first
5236
second
5236

Both trees have root five, left child two, right child three, and six as the left child of three, matching the source illustration.

Example 2

first = [1,2]second = [1,null,2]return = false
first
12
second
12

The value two occurs as a left child in one tree and a right child in the other.

Constraints

  • Each input is a valid binary tree with between 0 and 20000 nodes.
  • -10^9 <= node.val <= 10^9; values may repeat.

More Apple Onsite Interview questions

  • LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
  • Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • FIFO Mutex Operation TraceONSITE INTERVIEW · Seen Oct 2026
  • Implement Hit CounterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Parse Nested HTML into a TreeONSITE INTERVIEW · Seen Oct 2026
  • Thread-Safe Concurrent Linked ListONSITE INTERVIEW · Seen Sep 2026
  • Palindrome Match PercentageONSITE INTERVIEW · Seen Sep 2026
  • Binary Tree Nodes at Distance KONSITE INTERVIEW · Seen Aug 2026
  • BST to Sorted Circular Doubly Linked ListONSITE INTERVIEW · Seen Aug 2026

Similar questions at other companies

  • Diameter of Binary TreeeBay · ONSITE INTERVIEWeBay interview questions
  • Count One-Valued Regions in a Binary TreeGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Maximum Depth of Binary TreeLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
  • Path SumSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Binary Tree Preorder TraversalAnduril · ONSITE INTERVIEWAnduril interview questions
  • Root-to-Node Path in a Binary TreeAmazon · ONSITE INTERVIEW
  • Symmetric Binary TreeFigma · ONSITE INTERVIEWFigma interview questions
  • Tree Level with the Most NodesBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
  • Diameter of a Binary TreeMeta · ONSITE INTERVIEWMeta interview questions
  • Check That Every Node Equals Its Descendant SumMicrosoft · ONSITE INTERVIEWMicrosoft interview questions

Interview reports that mention this question

  • Apple India SWE New Grad Interviews: 5 Experiences

Apple interview experiences

  • Apple SWE Phone Screen: Subarray Products: 1 Experience
  • Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
  • Apple AI Coding and Related Interviews: 17 Experiences
  • Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
  • Apple New Grad SWE and Embedded: 16 Experiences (Part 2 of 3)
All Apple interview questionsSee Apple hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean sameTree(TreeNode first, TreeNode second) {
        // Write your solution here.
    }

}
Checking account…