Mark As Completed Discussion

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.

Description

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 and 1000000000
  • 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?

JAVASCRIPT
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.

Returning members can login to stop seeing this.