- Популярные видео
- Авто
- Видео-блоги
- ДТП, аварии
- Для маленьких
- Еда, напитки
- Животные
- Закон и право
- Знаменитости
- Игры
- Искусство
- Комедии
- Красота, мода
- Кулинария, рецепты
- Люди
- Мото
- Музыка
- Мультфильмы
- Наука, технологии
- Новости
- Образование
- Политика
- Праздники
- Приколы
- Природа
- Происшествия
- Путешествия
- Развлечения
- Ржач
- Семья
- Сериалы
- Спорт
- Стиль жизни
- ТВ передачи
- Танцы
- Технологии
- Товары
- Ужасы
- Фильмы
- Шоу-бизнес
- Юмор
Same Binary Tree Explained in 60 Seconds | LeetCode 100 | Java DSA Interview Question
Learn how to solve the Same Binary Tree problem (LeetCode 100) in just 60 seconds!
Given two binary trees, determine whether they are exactly the same.
For two trees to be considered identical:
✅ They must have the same structure
✅ They must contain the same node values
Example 1
Tree P Tree Q
1 1
/ \ / \
2 3 2 3
/ \ \ / \ \
4 5 6 4 5 6
Output: true
Example 2
Tree P Tree Q
1 1
/ \ / \
2 3 2 7
/ \ / \
4 5 4 5
Output: false
Java Recursive Solution
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
}
if (p == null || q == null) {
return false;
}
if (p.val != q.val) {
return false;
}
return isSameTree(p.left, q.left) &&
isSameTree(p.right, q.right);
}
📌 Key Points:
Both nodes null → true
One node null → false
Values different → false
Recursively compare left and right subtrees
⏱️ Time Complexity: O(n)
💾 Space Complexity: O(h)
Subscribe for more Java, LeetCode, Binary Tree, and DSA interview preparation content!
Видео Same Binary Tree Explained in 60 Seconds | LeetCode 100 | Java DSA Interview Question канала EverythingAboutJava
Given two binary trees, determine whether they are exactly the same.
For two trees to be considered identical:
✅ They must have the same structure
✅ They must contain the same node values
Example 1
Tree P Tree Q
1 1
/ \ / \
2 3 2 3
/ \ \ / \ \
4 5 6 4 5 6
Output: true
Example 2
Tree P Tree Q
1 1
/ \ / \
2 3 2 7
/ \ / \
4 5 4 5
Output: false
Java Recursive Solution
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
}
if (p == null || q == null) {
return false;
}
if (p.val != q.val) {
return false;
}
return isSameTree(p.left, q.left) &&
isSameTree(p.right, q.right);
}
📌 Key Points:
Both nodes null → true
One node null → false
Values different → false
Recursively compare left and right subtrees
⏱️ Time Complexity: O(n)
💾 Space Complexity: O(h)
Subscribe for more Java, LeetCode, Binary Tree, and DSA interview preparation content!
Видео Same Binary Tree Explained in 60 Seconds | LeetCode 100 | Java DSA Interview Question канала EverythingAboutJava
same binary tree same tree leetcode 100 binary tree java dsa binary tree interview questions dsa dsa interview preparation java programming tree recursion recursive solution java interview questions leetcode solutions coding interview data structures and algorithms binary tree algorithms software engineer tree problems binary tree java recursion in java coding shorts programming tutorials tech reels learn dsa leetcode java same tree problem
Комментарии отсутствуют
Информация о видео
12 ч. 12 мин. назад
00:01:45
Другие видео канала





















