Check Identical Binary Trees
Updated
Problem statement
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) → booleanExamples
Example 1
first = [5,2,3,null,null,6]second = [5,2,3,null,null,6]return = trueBoth 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 = falseThe 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
0and20000nodes. -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