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..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 @@ -246,37 +246,67 @@ 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]; } ``` +#### 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 375aa87e0889b..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 @@ -241,37 +241,67 @@ 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]; } ``` +#### 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]; +}; 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]; }