From 8c66e9dc371d9dce9316c38369b5281a57ff0b81 Mon Sep 17 00:00:00 2001 From: limlimjo Date: Sat, 1 Mar 2025 07:33:08 +0900 Subject: [PATCH 1/2] same tree solution --- same-tree/limlimjo.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 same-tree/limlimjo.js diff --git a/same-tree/limlimjo.js b/same-tree/limlimjo.js new file mode 100644 index 000000000..226ae9950 --- /dev/null +++ b/same-tree/limlimjo.js @@ -0,0 +1,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); +}; \ No newline at end of file From acd527134b5957975701b84beb9f268bf837b91d Mon Sep 17 00:00:00 2001 From: limlimjo Date: Sat, 1 Mar 2025 07:46:52 +0900 Subject: [PATCH 2/2] same tree solution-fix --- same-tree/limlimjo.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/same-tree/limlimjo.js b/same-tree/limlimjo.js index 226ae9950..43bee57d8 100644 --- a/same-tree/limlimjo.js +++ b/same-tree/limlimjo.js @@ -23,4 +23,4 @@ var isSameTree = function(p, q) { if (p.val != q.val) return false; return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); -}; \ No newline at end of file +};