LeetCode 100. Same Tree

Description

https://leetcode.com/problems/same-tree/description/

Given two binary trees, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical and the nodes have the same value.

Example 1:

Example 2:

Example 3:

Explanation

Our goal is to implement isSameTree(TreeNode p, TreeNode q) function.

There are two scenarios for isSameTree(TreeNode p, TreeNode q) to return true:

  1. p and q are both null
  2. when p and q aren’t null, p and q should have same value and p’s left and right subtrees should be same as q’s left and q.right subtrees.

Video Tutorial

Java Solution

Leave a Reply

Your email address will not be published. Required fields are marked *