From 9c1775d27675fbd1985d05a554031d677773e2d8 Mon Sep 17 00:00:00 2001 From: JiHo00 <110150963+jihostudy@users.noreply.github.com> Date: Fri, 19 Jul 2024 01:18:16 +0900 Subject: [PATCH] Day18 2Q JIHO --- .../Leetcode_567. Permutation in String.js | 0 .../Leetcode_76. Minimum Window Substring.js | 0 ...tcode_104. Maximum Depth of Binary Tree.js | 0 .../Leetcode_226. Invert Binary Tree.js | 0 .../Leetcode_543. Diameter of Binary Tree.js | 0 Jiho/Day18/Leetcode_100. Same Tree.js | 50 +++++++++++++++++++ .../Leetcode_110. Balanced Binary Tree.js | 32 ++++++++++++ 7 files changed, 82 insertions(+) rename Jiho/{Day14 => Day15}/Leetcode_567. Permutation in String.js (100%) rename Jiho/{Day14 => Day15}/Leetcode_76. Minimum Window Substring.js (100%) rename Jiho/{Day15 => Day16}/Leetcode_104. Maximum Depth of Binary Tree.js (100%) rename Jiho/{Day15 => Day16}/Leetcode_226. Invert Binary Tree.js (100%) rename Jiho/{Day15 => Day16}/Leetcode_543. Diameter of Binary Tree.js (100%) create mode 100644 Jiho/Day18/Leetcode_100. Same Tree.js create mode 100644 Jiho/Day18/Leetcode_110. Balanced Binary Tree.js diff --git a/Jiho/Day14/Leetcode_567. Permutation in String.js b/Jiho/Day15/Leetcode_567. Permutation in String.js similarity index 100% rename from Jiho/Day14/Leetcode_567. Permutation in String.js rename to Jiho/Day15/Leetcode_567. Permutation in String.js diff --git a/Jiho/Day14/Leetcode_76. Minimum Window Substring.js b/Jiho/Day15/Leetcode_76. Minimum Window Substring.js similarity index 100% rename from Jiho/Day14/Leetcode_76. Minimum Window Substring.js rename to Jiho/Day15/Leetcode_76. Minimum Window Substring.js diff --git a/Jiho/Day15/Leetcode_104. Maximum Depth of Binary Tree.js b/Jiho/Day16/Leetcode_104. Maximum Depth of Binary Tree.js similarity index 100% rename from Jiho/Day15/Leetcode_104. Maximum Depth of Binary Tree.js rename to Jiho/Day16/Leetcode_104. Maximum Depth of Binary Tree.js diff --git a/Jiho/Day15/Leetcode_226. Invert Binary Tree.js b/Jiho/Day16/Leetcode_226. Invert Binary Tree.js similarity index 100% rename from Jiho/Day15/Leetcode_226. Invert Binary Tree.js rename to Jiho/Day16/Leetcode_226. Invert Binary Tree.js diff --git a/Jiho/Day15/Leetcode_543. Diameter of Binary Tree.js b/Jiho/Day16/Leetcode_543. Diameter of Binary Tree.js similarity index 100% rename from Jiho/Day15/Leetcode_543. Diameter of Binary Tree.js rename to Jiho/Day16/Leetcode_543. Diameter of Binary Tree.js diff --git a/Jiho/Day18/Leetcode_100. Same Tree.js b/Jiho/Day18/Leetcode_100. Same Tree.js new file mode 100644 index 0000000..c7f81f1 --- /dev/null +++ b/Jiho/Day18/Leetcode_100. Same Tree.js @@ -0,0 +1,50 @@ +// p,q are roots of two binary trees +var isSameTree = function (p, q) { + // 두개다 null인 경우 + if (!p && !q) { + return true; + } + // 둘다 null이 아닌 경우 + else if (!p !== null && q !== null) { + if (p.val !== q.val) return false; + + if ( + (p.left !== null && q.left === null) || + (p.left === null && q.left !== null) + ) { + return false; + } + if ( + (p.right !== null && q.right === null) || + (p.right === null && q.right !== null) + ) { + return false; + } + return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); + } + // 둘중 하나만 null인 경우 + else { + return false; + } +}; + +// p,q are roots of two binary trees +var isSameTree = function (p, q) { + // #1. p and q are null + if (!p && !q) { + return true; + } + // #2. p and q should not have null + same value + else if (!p || !q || p.val !== q.val) { + return false; + } + // #3. Check left and right validation + else { + // 최적화 방법 + if (!isSameTree(p.left, q.left)) { + return false; + } + return isSameTree(p.right, q.right); + // return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); + } +}; diff --git a/Jiho/Day18/Leetcode_110. Balanced Binary Tree.js b/Jiho/Day18/Leetcode_110. Balanced Binary Tree.js new file mode 100644 index 0000000..56c9742 --- /dev/null +++ b/Jiho/Day18/Leetcode_110. Balanced Binary Tree.js @@ -0,0 +1,32 @@ +// 왼쪽깊이 구하기 + 오른쪽깊이 구하기 + +var isBalanced = function (root) { + if (!root) { + return true; + } + + // #1. 자신 + let l = countNodes(root.left); + let r = countNodes(root.right); + + if (Math.abs(l - r) > 1) { + return false; + } + // #2. 왼쪽 오른쪽 + return isBalanced(root.left) && isBalanced(root.right); +}; + +const countNodes = (root, depth = 1) => { + if (!root) { + return 0; + } + + if (!root.left && !root.right) { + return depth; + } + + return Math.max( + countNodes(root.left, depth + 1), + countNodes(root.right, depth + 1) + ); +};