forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimlimjo.js
More file actions
26 lines (22 loc) · 827 Bytes
/
limlimjo.js
File metadata and controls
26 lines (22 loc) · 827 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 시간복잡도: O(n) - 모든 노드를 한번씩 방문
// 공간복잡도: O(logn) - 균형 트리인 경우 / O(n) - 한쪽으로 치우친 트리인 경우
// 풀이: 재귀적으로 트리를 순회하면서 두 트리의 노드 값이 같은지 확인
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} p
* @param {TreeNode} q
* @return {boolean}
*/
var isSameTree = function(p, 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);
};