From 250e648930e80d4be2f6b6b552cde191e5fb0ff5 Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 15 Jul 2024 23:01:10 +0300 Subject: [PATCH 1/2] feat: update ts solution to lc problem: No.2196 --- .../README.md | 43 ++++++++----------- .../README_EN.md | 43 ++++++++----------- .../Solution.ts | 43 ++++++++----------- 3 files changed, 51 insertions(+), 78 deletions(-) diff --git a/solution/2100-2199/2196.Create Binary Tree From Descriptions/README.md b/solution/2100-2199/2196.Create Binary Tree From Descriptions/README.md index 16cddcc4ce3fa..b6ccd8f1d4869 100644 --- a/solution/2100-2199/2196.Create Binary Tree From Descriptions/README.md +++ b/solution/2100-2199/2196.Create Binary Tree From Descriptions/README.md @@ -246,34 +246,25 @@ func createBinaryTree(descriptions [][]int) *TreeNode { */ function createBinaryTree(descriptions: number[][]): TreeNode | null { - const map = new Map(); - const isRoot = new Map(); - for (const [parent, child, isLeft] of descriptions) { - let [left, right] = map.get(parent) ?? [0, 0]; - if (isLeft) { - left = child; - } else { - right = child; - } - if (!isRoot.has(parent)) { - isRoot.set(parent, true); - } - isRoot.set(child, false); - map.set(parent, [left, right]); + const nodes: Record = {}; + const children = new Set(); + + for (const [parent, child] of descriptions) { + if (!nodes[parent]) nodes[parent] = new TreeNode(parent); + if (!nodes[child]) nodes[child] = new TreeNode(child); + + children.add(child); } - const dfs = (val: number) => { - if (val === 0) { - return null; - } - const [left, right] = map.get(val) ?? [0, 0]; - return new TreeNode(val, dfs(left), dfs(right)); - }; - for (const [key, val] of isRoot.entries()) { - if (val) { - return dfs(key); - } + + let root = -1; + for (const [parent, child, isLeft] of descriptions) { + if (!children.has(parent)) root = parent; + + if (isLeft) nodes[parent].left = nodes[child]; + else nodes[parent].right = nodes[child]; } - return null; + + return nodes[root]; } ``` diff --git a/solution/2100-2199/2196.Create Binary Tree From Descriptions/README_EN.md b/solution/2100-2199/2196.Create Binary Tree From Descriptions/README_EN.md index 375aa87e0889b..24316225f9f14 100644 --- a/solution/2100-2199/2196.Create Binary Tree From Descriptions/README_EN.md +++ b/solution/2100-2199/2196.Create Binary Tree From Descriptions/README_EN.md @@ -241,34 +241,25 @@ func createBinaryTree(descriptions [][]int) *TreeNode { */ function createBinaryTree(descriptions: number[][]): TreeNode | null { - const map = new Map(); - const isRoot = new Map(); - for (const [parent, child, isLeft] of descriptions) { - let [left, right] = map.get(parent) ?? [0, 0]; - if (isLeft) { - left = child; - } else { - right = child; - } - if (!isRoot.has(parent)) { - isRoot.set(parent, true); - } - isRoot.set(child, false); - map.set(parent, [left, right]); + const nodes: Record = {}; + const children = new Set(); + + for (const [parent, child] of descriptions) { + if (!nodes[parent]) nodes[parent] = new TreeNode(parent); + if (!nodes[child]) nodes[child] = new TreeNode(child); + + children.add(child); } - const dfs = (val: number) => { - if (val === 0) { - return null; - } - const [left, right] = map.get(val) ?? [0, 0]; - return new TreeNode(val, dfs(left), dfs(right)); - }; - for (const [key, val] of isRoot.entries()) { - if (val) { - return dfs(key); - } + + let root = -1; + for (const [parent, child, isLeft] of descriptions) { + if (!children.has(parent)) root = parent; + + if (isLeft) nodes[parent].left = nodes[child]; + else nodes[parent].right = nodes[child]; } - return null; + + return nodes[root]; } ``` diff --git a/solution/2100-2199/2196.Create Binary Tree From Descriptions/Solution.ts b/solution/2100-2199/2196.Create Binary Tree From Descriptions/Solution.ts index 5ae7f108a5588..57dd64c8a53c8 100644 --- a/solution/2100-2199/2196.Create Binary Tree From Descriptions/Solution.ts +++ b/solution/2100-2199/2196.Create Binary Tree From Descriptions/Solution.ts @@ -13,32 +13,23 @@ */ function createBinaryTree(descriptions: number[][]): TreeNode | null { - const map = new Map(); - const isRoot = new Map(); - for (const [parent, child, isLeft] of descriptions) { - let [left, right] = map.get(parent) ?? [0, 0]; - if (isLeft) { - left = child; - } else { - right = child; - } - if (!isRoot.has(parent)) { - isRoot.set(parent, true); - } - isRoot.set(child, false); - map.set(parent, [left, right]); + const nodes: Record = {}; + const children = new Set(); + + for (const [parent, child] of descriptions) { + if (!nodes[parent]) nodes[parent] = new TreeNode(parent); + if (!nodes[child]) nodes[child] = new TreeNode(child); + + children.add(child); } - const dfs = (val: number) => { - if (val === 0) { - return null; - } - const [left, right] = map.get(val) ?? [0, 0]; - return new TreeNode(val, dfs(left), dfs(right)); - }; - for (const [key, val] of isRoot.entries()) { - if (val) { - return dfs(key); - } + + let root = -1; + for (const [parent, child, isLeft] of descriptions) { + if (!children.has(parent)) root = parent; + + if (isLeft) nodes[parent].left = nodes[child]; + else nodes[parent].right = nodes[child]; } - return null; + + return nodes[root]; } From 88526a3cc0d4fc779884b279f7eb83e3173331ed Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 15 Jul 2024 23:06:20 +0300 Subject: [PATCH 2/2] feat: add js solution to lc problem: No.2196 --- .../README.md | 39 +++++++++++++++++++ .../README_EN.md | 39 +++++++++++++++++++ .../Solution.js | 34 ++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 solution/2100-2199/2196.Create Binary Tree From Descriptions/Solution.js diff --git a/solution/2100-2199/2196.Create Binary Tree From Descriptions/README.md b/solution/2100-2199/2196.Create Binary Tree From Descriptions/README.md index b6ccd8f1d4869..9f717e5ca0485 100644 --- a/solution/2100-2199/2196.Create Binary Tree From Descriptions/README.md +++ b/solution/2100-2199/2196.Create Binary Tree From Descriptions/README.md @@ -268,6 +268,45 @@ function createBinaryTree(descriptions: number[][]): TreeNode | null { } ``` +#### JavaScript + +```js +/** + * 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[][]} descriptions + * @return {TreeNode} + */ + +var createBinaryTree = function (descriptions) { + const nodes = {}; + const children = new Set(); + + for (const [parent, child] of descriptions) { + if (!nodes[parent]) nodes[parent] = new TreeNode(parent); + if (!nodes[child]) nodes[child] = new TreeNode(child); + + children.add(child); + } + + let root = -1; + for (const [parent, child, isLeft] of descriptions) { + if (!children.has(parent)) root = parent; + + if (isLeft) nodes[parent].left = nodes[child]; + else nodes[parent].right = nodes[child]; + } + + return nodes[root]; +}; +``` + #### Rust ```rust diff --git a/solution/2100-2199/2196.Create Binary Tree From Descriptions/README_EN.md b/solution/2100-2199/2196.Create Binary Tree From Descriptions/README_EN.md index 24316225f9f14..ebf61f090da70 100644 --- a/solution/2100-2199/2196.Create Binary Tree From Descriptions/README_EN.md +++ b/solution/2100-2199/2196.Create Binary Tree From Descriptions/README_EN.md @@ -263,6 +263,45 @@ function createBinaryTree(descriptions: number[][]): TreeNode | null { } ``` +#### JavaScript + +```js +/** + * 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[][]} descriptions + * @return {TreeNode} + */ + +var createBinaryTree = function (descriptions) { + const nodes = {}; + const children = new Set(); + + for (const [parent, child] of descriptions) { + if (!nodes[parent]) nodes[parent] = new TreeNode(parent); + if (!nodes[child]) nodes[child] = new TreeNode(child); + + children.add(child); + } + + let root = -1; + for (const [parent, child, isLeft] of descriptions) { + if (!children.has(parent)) root = parent; + + if (isLeft) nodes[parent].left = nodes[child]; + else nodes[parent].right = nodes[child]; + } + + return nodes[root]; +}; +``` + #### Rust ```rust diff --git a/solution/2100-2199/2196.Create Binary Tree From Descriptions/Solution.js b/solution/2100-2199/2196.Create Binary Tree From Descriptions/Solution.js new file mode 100644 index 0000000000000..82f519329a434 --- /dev/null +++ b/solution/2100-2199/2196.Create Binary Tree From Descriptions/Solution.js @@ -0,0 +1,34 @@ +/** + * 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[][]} descriptions + * @return {TreeNode} + */ + +var createBinaryTree = function (descriptions) { + const nodes = {}; + const children = new Set(); + + for (const [parent, child] of descriptions) { + if (!nodes[parent]) nodes[parent] = new TreeNode(parent); + if (!nodes[child]) nodes[child] = new TreeNode(child); + + children.add(child); + } + + let root = -1; + for (const [parent, child, isLeft] of descriptions) { + if (!children.has(parent)) root = parent; + + if (isLeft) nodes[parent].left = nodes[child]; + else nodes[parent].right = nodes[child]; + } + + return nodes[root]; +};