Skip to content

Commit 66a9725

Browse files
committed
same tree solution
1 parent b57c9b3 commit 66a9725

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

same-tree/byol-han.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* https://leetcode.com/problems/same-tree/description/
3+
* Definition for a binary tree node.
4+
* function TreeNode(val, left, right) {
5+
* this.val = (val===undefined ? 0 : val)
6+
* this.left = (left===undefined ? null : left)
7+
* this.right = (right===undefined ? null : right)
8+
* }
9+
*/
10+
/**
11+
* @param {TreeNode} p
12+
* @param {TreeNode} q
13+
* @return {boolean}
14+
*/
15+
var isSameTree = function (p, q) {
16+
// 둘 다 null이면 같은 트리
17+
if (p === null && q === null) return true;
18+
19+
// 하나는 null이고 하나는 값이 있다면 다른 트리
20+
if (p === null || q === null) return false;
21+
22+
// 값이 다르면 다른 트리
23+
if (p.val !== q.val) return false;
24+
25+
// 왼쪽과 오른쪽 서브트리도 각각 재귀적으로 비교
26+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
27+
};

0 commit comments

Comments
 (0)