-
[75 LeetCode] 18 - Same TreeStudy/Leetcode 2023. 4. 27. 00:50
[75 LeetCode]는 코딩테스트 연습을 위해 한 페이스북 개발자가 추천하는 75가지 알고리즘 문제를 풀어보는 시리즈이다.
블라인드 원문:
https://www.teamblind.com/post/New-Year-Gift---Curated-List-of-Top-75-LeetCode-Questions-to-Save-Your-Time-OaM1orEU문제 링크: https://leetcode.com/problems/same-tree/description/
Same Tree - LeetCode
Can you solve this real interview question? Same Tree - Given the roots of two binary trees p and q, 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
leetcode.com
같은 트리를 찾는 쉬운 문제.
현재 노드의 값이 같고, l, r 트리 각각에 대해서도 같다면 같은 트리이다.
재귀로 간단히 풀 수 있다.var isSameTree = function(p, q) { if (p === q) return true; if (p === null || q === null) return false; if (p.val !== q.val) return false; if (p.val === q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right)) return true; else return false };
728x90'Study > Leetcode' 카테고리의 다른 글
[75 LeetCode] 20 - Contains Duplicate (0) 2023.04.27 [75 LeetCode] 19 - Invert Binary Tree (0) 2023.04.27 [75 LeetCode] 17 - Longest Repeating Character Replacement (0) 2023.04.27 [75 LeetCode] 16 - Linked List Cycle (0) 2023.04.27 [75 LeetCode] 15 - Merge Intervals (1) 2023.04.27