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 3The 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
-1000000000and1000000000 - 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?
xxxxxxxxxx78
​def identical_trees(tree1, tree2): # fill in return tree1​​# Node definitionclass Node: def __init__(self, val): self.val = val self.left = None self.right = None​​# Regular binary treestree1 = Node(4)tree1.left = Node(1)tree1.right = Node(3)​tree2 = Node(5)tree2.left = Node(10)tree2.left.left = Node(17)tree2.left.right = Node(3)tree2.right = Node(8)​# Binary search treestree3 = Node(6)tree3.left = Node(3)​tree4 = Node(5)OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
Here's a video of us explaining the solution.
To change the speed of the video or see it in full screen, click the icons to the right of the progress bar.

We'll now take you through what you need to know.
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.

