File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 시간복잡도: O(n) - 모든 노드를 한번씩 방문
2
+ // 공간복잡도: O(logn) - 균형 트리인 경우 / O(n) - 한쪽으로 치우친 트리인 경우
3
+ // 풀이: 재귀적으로 트리를 순회하면서 두 트리의 노드 값이 같은지 확인
4
+
5
+ /**
6
+ * Definition for a binary tree node.
7
+ * function TreeNode(val, left, right) {
8
+ * this.val = (val===undefined ? 0 : val)
9
+ * this.left = (left===undefined ? null : left)
10
+ * this.right = (right===undefined ? null : right)
11
+ * }
12
+ */
13
+ /**
14
+ * @param {TreeNode } p
15
+ * @param {TreeNode } q
16
+ * @return {boolean }
17
+ */
18
+ var isSameTree = function ( p , q ) {
19
+ if ( p === null && q === null ) return true ;
20
+
21
+ if ( p === null || q === null ) return false ;
22
+
23
+ if ( p . val != q . val ) return false ;
24
+
25
+ return isSameTree ( p . left , q . left ) && isSameTree ( p . right , q . right ) ;
26
+ } ;
You can’t perform that action at this time.
0 commit comments