Identical Trees
Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.
Description
Can you write a function to see if two binary trees are identical?
1 1
/ \ / \
2 3 2 3
The above two trees share the same structure in terms of shape and location of each parent nodes' children.

Additionally, each corresponding nodes at every position has the exact same value. Thus, the above is consideredidentical.
The below two are not identical, because we are missing a child node.
1 1
/ \ /
1 3 3
The definition of a tree node is as follows:
function Node(val) {
this.val = val;
this.left = null;
this.right = null;
}
Constraints
- Number of vertices in both the trees <=
100000 - The trees can also be null
- The values of the vertices in the tree will be between
-1000000000and1000000000 - Expected time complexity :
O(n) - Expected space complexity :
O(n)
```js
function identicalTrees(tree1, tree2) {
// fill this in
return true;
}
```
CONTINUE WITH ALGODAILY PREMIUM
Keep going with the complete tutorial.
Unlock the remaining explanations and exercises, alongside the full learning library.
Explore Premium →