Clone Graph

原创 2016年05月31日 18:33:02

updated: 06/10, 我们必须深刻理解什么是clone,不要将引用克隆

curNodeClone.neighbors.add(curNodeNeighborClone);
加入的是clone,而不是现有的引用

典型的bfs遍历问题,基本功

很棒的代码点击打开链接

/**
     * @param node: A undirected graph node
     * @return: A undirected graph node
     */
    public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
        if (node == null) {
            return null;
        }
        // 放原始node和其复制品  
        Map<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<>();
        // BFS用的queue
        List<UndirectedGraphNode> list = new LinkedList<UndirectedGraphNode>(); 
        // 根节点的复制
        UndirectedGraphNode nodeClone = new UndirectedGraphNode(node.label);
        // 把根节点和其复制品放入map 
        map.put(node, nodeClone);
        //添加入队列  
        list.add(node);
        while (!list.isEmpty()) {
            // 当前处理对象
            UndirectedGraphNode curNode = list.remove(0);
            // 得到当前原始对象的所有neighbor  
            ArrayList<UndirectedGraphNode> curNodeNeighbors = curNode.neighbors;
            // 当前处理对象的复制品,必定在map里,因为在前面的neighbor里已经被创建
            UndirectedGraphNode curNodeClone = map.get(curNode);
            for (UndirectedGraphNode curNodeNeighbor: curNodeNeighbors) {
                if (map.containsKey(curNodeNeighbor)) {
                    //UndirectedGraphNode curNodeClone = map.get(curNode);
                    // 之前已经被复制过的neighbor
                    UndirectedGraphNode curNodeNeighborClone = map.get(curNodeNeighbor);
                    // 就直接从map里取出neighborClone, 给curClone添加复制的neighbor
                    curNodeClone.neighbors.add(curNodeNeighborClone);
                } else {
                    // 如果该neighbor没有被复制过,则新建neighborClone
                    UndirectedGraphNode curNodeNeighborClone = new UndirectedGraphNode(curNodeNeighbor.label);
                    //UndirectedGraphNode curNodeClone = map.get(curNode);
                    curNodeClone.neighbors.add(curNodeNeighborClone);
                    // 并且添加到队列里为了将来的遍历
                    list.add(curNodeNeighbor);
                    // 存储到map里
                    map.put(curNodeNeighbor, curNodeNeighborClone);
                }
            }
        }
        return nodeClone;
    }



LeetCode 19 Clone Graph

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.

LeetCode_Clone Graph

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...

【leetcode】Clone Graph(python)

类似于二叉树的三种遍历,我们可以基于遍历的模板做很多额外的事情,图的两种遍历,深度和广度模板同样也可以做很多额外的事情,这里举例利用深度优先遍历的模板来进行复制,深度优先中,我们先访问第一个结点,接着...

算法设计Week3 LeetCode Algorithms Problem #133 Clone Graph

题目描述: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbo...

133. Clone Graph(DFS)

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ...

[C++]LeetCode: 129 Clone Graph (图的深拷贝 BFS && DFS)

题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbor...

Clone Graph -- leetcode

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...

LeetCode133. Clone Graph

题目: https://leetcode.com/problems/clone-graph/ Clone an undirected graph. Each node in the graph c...
  • codeTZ
  • codeTZ
  • 2016年03月22日 20:04
  • 223

[Leetcode]Clone Graph

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ...

LeetCode: Clone Graph [133]

【题目】 Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Clone Graph
举报原因:
原因补充:

(最多只允许输入30个字)