Good morning! Here's our prompt for today.
Can you write a function to see if two binary trees are identical?
SNIPPET
1 1 1
2 / \ / \
3 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.
SNIPPET
1 1 1
2 / \ /
3 1 3 3
The definition of a tree node is as follows:
JAVASCRIPT
1function Node(val) {
2 this.val = val;
3 this.left = null;
4 this.right = null;
5}
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
-1000000000
and1000000000
- Expected time complexity :
O(n)
- Expected space complexity :
O(n)
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
74
'PASSED: ' + 'assert.deepEqual(identicalTrees(tree3, tree3Clone), true);'
var assert = require('assert');
​
function identicalTrees(tree1, tree2) {
// fill this in
return true;
}
​
function Node(val) {
this.val = val;
this.left = null;
this.right = null;
}
​
// Regular binary trees
var tree1 = new Node(4);
tree1.left = new Node(1);
tree1.right = new Node(3);
​
var tree2 = new Node(5);
tree2.left = new Node(10);
tree2.left.left = new Node(17);
tree2.left.right = new Node(3);
tree2.right = new Node(8);
​
// Binary search trees
var tree3 = new Node(6);
tree3.left = new Node(3);
​
var tree4 = new Node(5);
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
Tired of reading? Watch this video explanation!
To change the speed of the video or see it in full screen, click the icons to the right of the progress bar.

Here's our guided, illustrated walk-through.
How do I use this guide?
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.