forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyoungduck.js
More file actions
32 lines (27 loc) ยท 891 Bytes
/
youngduck.js
File metadata and controls
32 lines (27 loc) ยท 891 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
27
28
29
30
31
32
/**
* 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) {
// ํธ๋ฆฌ ์ํ -> ์ฌ๊ท, ํ, ์คํ ๋ฐฉ์๋ค์ด ์์.
// ๋๊ฐ์ ํธ๋ฆฌ๋ฅผ ๋์์ ์กฐํํ๋ ํ์ด
// ๋ ๋ค null์ด๋ฉด ๊ฐ์
if (p === null && q === null) return true;
// ํ๋๋ง null์ด๋ฉด ๋ค๋ฆ
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);
};
// ์๊ฐ๋ณต์ก๋ O(N)
// ๊ณต๊ฐ๋ณต์ก๋ O(N)