diff --git "a/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Binary_Tree_Inorder_Traversal.js" "b/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Binary_Tree_Inorder_Traversal.js" new file mode 100644 index 0000000..57bec16 --- /dev/null +++ "b/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Binary_Tree_Inorder_Traversal.js" @@ -0,0 +1,23 @@ +/** + * 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} root + * @return {number[]} + */ +var inorderTraversal = function (root) { + const result = []; + function traverse(node) { + if (!node) return; + traverse(node.left); + result.push(node.val); + traverse(node.right); + } + traverse(root); + return result; +}; diff --git "a/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Binary_Tree_Level_Order_Traversal.js" "b/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Binary_Tree_Level_Order_Traversal.js" new file mode 100644 index 0000000..b0c7905 --- /dev/null +++ "b/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Binary_Tree_Level_Order_Traversal.js" @@ -0,0 +1,26 @@ +/** + * 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} root + * @return {number[][]} + */ +var levelOrder = function (root) { + const result = []; + + function traverse(node, level) { + if (!node) return; + if (!result[level]) result[level] = []; + result[level].push(node.val); + traverse(node.left, level + 1); + traverse(node.right, level + 1); + } + + traverse(root, 0); + return result; +}; diff --git "a/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Binary_Tree_Maximum_Path_Sum.js" "b/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Binary_Tree_Maximum_Path_Sum.js" new file mode 100644 index 0000000..12f7554 --- /dev/null +++ "b/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Binary_Tree_Maximum_Path_Sum.js" @@ -0,0 +1,31 @@ +/** + * 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} root + * @return {number} + */ +var maxPathSum = function (root) { + let maxSum = -Infinity; + + function dfs(node) { + if (!node) return 0; + + const left = Math.max(dfs(node.left), 0); + const right = Math.max(dfs(node.right), 0); + + const currentSum = node.val + left + right; + + maxSum = Math.max(maxSum, currentSum); + + return node.val + Math.max(left, right); + } + + dfs(root); + return maxSum; +}; diff --git "a/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.js" "b/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.js" new file mode 100644 index 0000000..50b3860 --- /dev/null +++ "b/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.js" @@ -0,0 +1,38 @@ +/** + * 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 {number[]} preorder + * @param {number[]} inorder + * @return {TreeNode} + */ +var buildTree = function (preorder, inorder) { + const inorderMap = new Map(); + inorder.forEach((val, idx) => inorderMap.set(val, idx)); + + function build(preStart, preEnd, inStart, inEnd) { + if (preStart > preEnd || inStart > inEnd) return null; + + const rootVal = preorder[preStart]; + const root = new TreeNode(rootVal); + const inRootIndex = inorderMap.get(rootVal); + const leftSize = inRootIndex - inStart; + + root.left = build( + preStart + 1, + preStart + leftSize, + inStart, + inRootIndex - 1 + ); + root.right = build(preStart + leftSize + 1, preEnd, inRootIndex + 1, inEnd); + + return root; + } + + return build(0, preorder.length - 1, 0, inorder.length - 1); +}; diff --git "a/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Invert_Binary_Tree.js" "b/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Invert_Binary_Tree.js" new file mode 100644 index 0000000..9cff07e --- /dev/null +++ "b/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Invert_Binary_Tree.js" @@ -0,0 +1,26 @@ +/** + * 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} root + * @return {TreeNode} + */ +var invertTree = function (root) { + if (root === null) { + return null; + } + + let temp = root.left; + root.left = root.right; + root.right = temp; + + invertTree(root.left); + invertTree(root.right); + + return root; +}; diff --git "a/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Maximum_Depth_of_Binary_Tree.js" "b/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Maximum_Depth_of_Binary_Tree.js" new file mode 100644 index 0000000..e676b5c --- /dev/null +++ "b/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Maximum_Depth_of_Binary_Tree.js" @@ -0,0 +1,22 @@ +/** + * 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} root + * @return {number} + */ +var maxDepth = function (root) { + if (root === null) { + return 0; + } + + let leftDepth = maxDepth(root.left); + let rightDepth = maxDepth(root.right); + + return Math.max(leftDepth, rightDepth) + 1; +}; diff --git "a/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Same_Tree.js" "b/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Same_Tree.js" new file mode 100644 index 0000000..37b5b14 --- /dev/null +++ "b/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Same_Tree.js" @@ -0,0 +1,28 @@ +/** + * 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); +}; diff --git "a/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Validate_Binary_Search_Tree.js" "b/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Validate_Binary_Search_Tree.js" new file mode 100644 index 0000000..8639543 --- /dev/null +++ "b/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/Validate_Binary_Search_Tree.js" @@ -0,0 +1,24 @@ +/** + * 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} root + * @return {boolean} + */ +var isValidBST = function (root) { + function validate(node, lower, upper) { + if (!node) return true; + + const val = node.val; + if (val <= lower || val >= upper) return false; + + return validate(node.left, lower, val) && validate(node.right, val, upper); + } + + return validate(root, -Infinity, Infinity); +}; diff --git "a/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/\354\235\264\354\247\204_\353\263\200\355\231\230_\353\260\230\353\263\265\355\225\230\352\270\260.js" "b/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/\354\235\264\354\247\204_\353\263\200\355\231\230_\353\260\230\353\263\265\355\225\230\352\270\260.js" new file mode 100644 index 0000000..bb632c8 --- /dev/null +++ "b/oh-chaeyeon/5\354\243\274\354\260\250_\355\212\270\353\246\254/\354\235\264\354\247\204_\353\263\200\355\231\230_\353\260\230\353\263\265\355\225\230\352\270\260.js" @@ -0,0 +1,17 @@ +function solution(s) { + let count = 0; + let removed = 0; + + while (s !== "1") { + const originalLength = s.length; + const newStr = s + .split("") + .filter((char) => char === "1") + .join(""); + removed += originalLength - newStr.length; + s = newStr.length.toString(2); + count++; + } + + return [count, removed]; +}