Leetcode OJ 133 Questions: Clone Graph

Source: Internet
Author: User

The topic is very long, in fact, only need to focus on the first line is OK. This problem thinking is quite obvious, for the diagram is either BFS, or DFS, as to the specifics, I think with 138: Copy List with the Random pointer very much like. During a BFS or DFS process, you may want to adjust the vertex adjacency point, and don't forget that its adjacency point may not have been created yet. So, there are the following ideas:

Traverse two times without a graph. The first time, all nodes are created without preserving the relationship between vertices. Second time, adjust the relationship between vertices. The relationship between vertices is saved in neighbors, in order to be able to find the position of the newly created vertex, the first pass time needs to preserve the one by one correspondence between the original vertex and the new vertex pointer, which can be saved by map or unordered_map.

I'm a little verbose here, so look at the comments in the code to make it clear. Here, I use BFS traversal. The time complexity is O (n) and the spatial complexity is O (n).

Http://www.lofter.com/favblog/xuyiomvlc909806?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/ Gongsongrrzux29?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/jiluyrcsg109459?fpost=1d26b0e9_6a01fd0 http ://www.lofter.com/favblog/wangtanwqwyu024?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/shuidongqisnz76? FPOST=1D26B0E9_6A01FD0 http://www.lofter.com/favblog/michoumdzsp8231?fpost=1d26b0e9_6a01fd0/HTTP Www.lofter.com/favblog/guodiuogep39547?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/weizhaogodgm695? FPOST=1D26B0E9_6A01FD0 http://www.lofter.com/favblog/anwoasoja157325?fpost=1d26b0e9_6a01fd0/HTTP Www.lofter.com/favblog/weishunlseng710?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/caizhongxgweo57? FPOST=1D26B0E9_6A01FD0 http://www.lofter.com/favblog/shutongcznps306?fpost=1d26b0e9_6a01fd0/HTTP Www.lofter.com/favblog/renmenlduge2803?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/manemwbfg542858? Fpost=1d26b0e9_6a01fd0 Http://www.lofter.com/favblog/jicoNGASDMN6365?FPOST=1D26B0E9_6A01FD0 http://www.lofter.com/favblog/pairangna8203?fpost=1d26b0e9_6a01fd0/HTTP www.lofter.com/favblog/zhuobikvdji1439?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/jichenghszjv098? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/chaijildmhl2590?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/ouyongiswwx7705?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/fuhangjoqxa0051? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/baolangakmwq726?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/shizhikdacd2637?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yanhuaibzawt014? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/jirenvrcef81309?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/daidaowbzxs8484?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/rongoukrtuj3385? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/fengmeitmffa771?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/fenglinemldj651?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yuanzhifygqv165?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/ cailuqfeim09860?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/mieboqdjmi58267?fpost=1d26a56f_6a0077c http ://www.lofter.com/favblog/moshangvfbna259?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/kongyuncrhix607? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/quqitbclo117894?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/raoshanabjek894?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/niuyoujerib0378? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/haorenknoab6213?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/yanzenbqkod6504?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gandengdllzr709? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/guodiffunb97223?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/minbigapof86162?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/fanliangcduuy54? fpost=1d26a56f_6a0077c Http://www.lofter.com/favblog/buchunboauk9810?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/shencangtwybd07?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/lianfeiyflun841?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/naqieivcjh08483? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/pangjiaoxzgdt70?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/pipangjdyni8384?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/xugaiwulpr80279? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/xianbeibtlkm830?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/nongtangqmmg769?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/zhuchengjxlgb61? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/changshitdpga04?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/sangturewxd0234?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yezhuanggvcer86? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yuexineuoba8918?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/zhaoxinrwlbz437?fpost=1d26a56f_6a0077chttp://www.lofter.com/favblog/wupioitpl903373?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/ kuishanggylrc36?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/shangweibqlji73?fpost=1d26a56f_6a0077c http ://www.lofter.com/favblog/yangxianddhes07?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yangcaiuxaem184? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/baidaosntec8062?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/douanodtsz49703?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yuqianbeicy9190? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/shuangshiayahu5?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/changnaopjtym49?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/haoluxnldz65291? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gousouvtjjq3553?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/zhuoyouvpnsu115?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/pingweitzvhl353? fpost=1d26a56f_6a0077c Http://www.lofter.com/favblog/ruantuniuwyl382?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/lumeilkzna25713?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/suweigdwup20495?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/ruicukfwmm19085? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/shenchenbzots52?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/duyuebawea15981?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/xiangguofiwa183? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/baishanghjrx354?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/huochenzxtag392?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gengshanpciuw42? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/bugubqpto458787?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/yunaioldgl66967?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/longcanglyrxw77? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yuanzazfsvs4188?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/huihanxvlmb3915?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/tongjitkzbj1208?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/ qudongvnsms8711?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gudongdnlie0746?fpost=1d26a56f_6a0077c http ://www.lofter.com/favblog/yiqiaokkxim4405?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/laocvhhu9726338? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/baomeiguttv6682?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/chaoliaoapucj78?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yanbeiwdrtx5074? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/lumorekei229629?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/keluanqnlic1065?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yuyoutgflp66710? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/putangcwkai6962?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/gushuikkslg8649?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/bushigpfvk63124? fpost=1d26a56f_6a0077c Http://www.lofter.com/favblog/zhengguahryna73?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/qiuwencqizl6631?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/fengbenhutht210?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/huiyuanedxnl983? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/jiangyongclqst8?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/pibaumrps460281?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/wangdanvhzqb429? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/zhumeiaewug9211?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/qitangkoyvl4260?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/qiliaodcdwy6377? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gucudqzti910355?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/baoqinbkpil0100?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/limieiewhm51997? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/zhuobikvdji1439?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/jichenghszjv098?fpost=1d26a56f_6a0077C http://www.lofter.com/favblog/chaijildmhl2590?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/ ouyongiswwx7705?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/fuhangjoqxa0051?fpost=1d26a56f_6a0077c http ://www.lofter.com/favblog/baolangakmwq726?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/shizhikdacd2637? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yanhuaibzawt014?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/jirenvrcef81309?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/daidaowbzxs8484? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/rongoukrtuj3385?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/fengmeitmffa771?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/fenglinemldj651? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yuanzhifygqv165?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/cailuqfeim09860?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/mieboqdjmi58267? fpost=1d26a56f_6a0077c Http://www.lofter.com/favblog/moshangvfbna259?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/kongyuncrhix607?fpost=1d26a56f_6a0077c http:/ /www.lofter.com/favblog/quqitbclo117894?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/raoshanabjek894? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/niuyoujerib0378?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/haorenknoab6213?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yanzenbqkod6504? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gandengdllzr709?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/guodiffunb97223?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/minbigapof86162? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/fanliangcduuy54?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/buchunboauk9810?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/shencangtwybd07? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/lianfeiyflun841?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/naqieivcjh08483?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/pangjiaoxzgdt70?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/ pipangjdyni8384?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/xugaiwulpr80279?fpost=1d26a56f_6a0077c http ://www.lofter.com/favblog/xianbeibtlkm830?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/nongtangqmmg769? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/zhuchengjxlgb61?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/changshitdpga04?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/sangturewxd0234? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yezhuanggvcer86?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/yuexineuoba8918?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/zhaoxinrwlbz437? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/wupioitpl903373?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/kuishanggylrc36?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/shangweibqlji73? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yangxianddhes07?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yangcaiuxaem184?fpost=1d26a56f_6a0077c http ://www.lofter.com/favblog/baidaosntec8062?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/douanodtsz49703? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yuqianbeicy9190?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/shuangshiayahu5?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/changnaopjtym49? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/haoluxnldz65291?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/gousouvtjjq3553?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/zhuoyouvpnsu115? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/pingweitzvhl353?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/ruantuniuwyl382?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/lumeilkzna25713? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/suweigdwup20495?fpost=1d26a56f_6a0077c/HTTP Www.lofter.com/favblog/ruicukfwmm19085?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/shenchenbzots52?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/ duyuebawea15981?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/xiangguofiwa183?fpost=1d26a56f_6a0077c http ://www.lofter.com/favblog/baishanghjrx354?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/huochenzxtag392? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gengshanpciuw42?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/bugubqpto458787?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yunaioldgl66967? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/longcanglyrxw77?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/yuanzazfsvs4188?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/huihanxvlmb3915? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/tongjitkzbj1208?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/qudongvnsms8711?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gudongdnlie0746? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yiqiaokkxim4405?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/laocvhhu9726338?fpost=1d26a56f_6a0077c http ://www.lofter.com/favblog/baomeiguttv6682?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/chaoliaoapucj78? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yanbeiwdrtx5074?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/lumorekei229629?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/keluanqnlic1065? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yuyoutgflp66710?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/putangcwkai6962?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gushuikkslg8649? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/bushigpfvk63124?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/zhengguahryna73?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/qiuwencqizl6631? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/fengbenhutht210?fpost=1d26a56f_6a0077c/HTTP Www.lofter.com/favblog/huiyuanedxnl983?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/jiangyongclqst8?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/ pibaumrps460281?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/wangdanvhzqb429?fpost=1d26a56f_6a0077c http ://www.lofter.com/favblog/zhumeiaewug9211?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/qitangkoyvl4260? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/qiliaodcdwy6377?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/gucudqzti910355?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/baoqinbkpil0100? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/limieiewhm51997?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/yinshiilcbd4060?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gongfenayett527? fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/jiajiaobhmfx280?fpost=1d26a56f_6a0077c/HTTP www.lofter.com/favblog/haoquekwitg9496?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gugoulosji05828? fpost=1d26a56f_6a0077c Http://www.lofter.com/favblog/gaocuxmrul38382?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/shaorongtvswx79?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/suxiauwfpi51126?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/ shizhangxgtqq51?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/biexinbjtwm3188?fpost=1d26a56f_6a0077c

Leetcode OJ 133 Questions: Clone Graph

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.