100. Same Tree
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to determine if two binary trees, p and q, are identical. For two trees to be identical, they must satisfy two conditions:
Solution
Same Tree in Python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
from typing import Optional
class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
# 1. If both nodes are None, they are identical.
if not p and not q:
return True
# 2. If one of the nodes is None or their values are different, they are not identical.
# This also covers the case where one is None and the other isn't.
if not p or not q or p.val != q.val:
return False
# 3. Recursively check the left and right subtrees.
# The `and` operator ensures that both checks must pass.
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
Same Tree in Java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
// 1. Base case: If both nodes are null, they are the same at this position.
if (p == null && q == null) {
return true;
}
// 2. If one of the nodes is null or their values are different, they are not the same.
// This check implicitly handles the case where one is null and the other isn't.
if (p == null || q == null || p.val != q.val) {
return false;
}
// 3. Recursively check the left and right children.
// Both the left and right subtrees must be identical for the trees to be the same.
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
Same Tree in C++
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
// 1. Base case: If both nodes are null, they are considered the same.
if (!p && !q) {
return true;
}
// 2. If one of the nodes is null, or if their values don't match,
// they are not the same. This also covers the case where one is null and the other isn't.
if (!p || !q || p->val != q->val) {
return false;
}
// 3. Recursively check if the left and right subtrees are the same.
// Both must be true for the entire trees to be the same.
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
};
Same Tree in TypeScript
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
// 1. Base case: If both nodes are null, they are considered the same.
if (!p && !q) {
return true;
}
// 2. If one of the nodes is null, or if their values don't match,
// they are not the same. This also covers the case where one is null and the other isn't.
if (!p || !q || p.val !== q.val) {
return false;
}
// 3. Recursively check if the left and right subtrees are the same.
// Both must be true for the entire trees to be the same.
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.