Skip to content

Commit 424cc78

Browse files
authored
[ PS ] : Same Tree
1 parent 1ef9a64 commit 424cc78

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

same-tree/uraflower.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} p
11+
* @param {TreeNode} q
12+
* @return {boolean}
13+
*/
14+
const isSameTree = function(p, q) {
15+
if (!p && !q) return true;
16+
return p?.val === q?.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
17+
};
18+
19+
// 시간복잡도: O(n)
20+
// 공간복잡도: O(h) (h: 트리의 높이, 즉 재귀 호출 스택)

0 commit comments

Comments
 (0)