GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • LeetCodeJavascript.com
  • Star on GitHub

This view

  • Original page
  • New address

GeezerVison is showing leetcodejavascript.com — #1485 - Clone Binary Tree With Random Pointer - LeetCode JavaScript Solutions

Back to all solutions

#1485 - Clone Binary Tree With Random Pointer

Medium • View on LeetCode • View on GitHub
Hash Table Depth-First Search Tree Breadth-First Search Binary Tree

Problem Description

A binary tree is given such that each node contains an additional random pointer which could point to any node in the tree or null.

Return a deep copy of the tree.

The tree is represented in the same input/output way as normal binary trees where each node is represented as a pair of [val, random_index] where:

  • val: an integer representing Node.val
  • random_index: the index of the node (in the input) where the random pointer points to, or null if it does not point to any node.

You will be given the tree in class Node and you should return the cloned tree in class NodeCopy. NodeCopy class is just a clone of Node class with the same attributes and constructors.

Solution

/**
 * // Definition for a _Node.
 * function _Node(val, left, right, random) {
 *    this.val = val === undefined ? null : val;
 *    this.left = left === undefined ? null : left;
 *    this.right = right === undefined ? null : right;
 *    this.random = random === undefined ? null : random;
 * };
 */

/**
 * @param {_Node} root
 * @return {NodeCopy}
 */
var copyRandomBinaryTree = function(root) {
  if (!root) return null;
  const nodeMap = new Map();
  return createCopy(root);

  function createCopy(node) {
    if (!node) return null;
    if (nodeMap.has(node)) return nodeMap.get(node);

    const copy = new NodeCopy(node.val);
    nodeMap.set(node, copy);

    copy.left = createCopy(node.left);
    copy.right = createCopy(node.right);
    copy.random = createCopy(node.random);

    return copy;
  }
};
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.