File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments