浜屽弶鏍戠殑搴忓垪鍖栦笌鍙嶅簭鍒楀寲

Posted Coding鐨勫摂鍝斿彣鍙?/a>

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了浜屽弶鏍戠殑搴忓垪鍖栦笌鍙嶅簭鍒楀寲相关的知识,希望对你有一定的参考价值。

馃檱馃憖馃檱鏍戠殑閬嶅巻涓€鑸湁涓や釜绛栫暐锛?/span>BFS / DFS銆?/span>

  • BFS 骞垮害浼樺厛鎼滅储锛屾寜鐓ф爲鐨勫眰娆¢『搴忎粠涓婂埌涓嬮亶鍘嗘墍鏈夌殑鑺傜偣銆?/span>

  • DFS娣卞害浼樺厛鎼滅储锛屼粠鏍戠殑鏍硅妭鐐瑰紑濮嬶紝鍚戜竴涓柟鍚戜竴鐩村欢浼稿埌鏌愪釜鍙跺瓙鑺傜偣锛岀劧鍚庡洖鍒版牴锛岃烦鍒板彟涓€涓瓙鏍戜笂锛岀户缁亶鍘嗐€?/span>

鏍规嵁鏍硅妭鐐广€佸乏鍙冲瓙鑺傜偣鐨勮緭鍑洪『搴忥紝鍙互灏嗘爲鐨勯亶鍘嗗垎涓猴細

  • 鍓嶅簭閬嶅巻

  • 涓簭閬嶅巻

  • 鍚庡簭閬嶅巻


馃憖馃挰浠婃棩鐨勭粌涔狅紝浜屽弶鏍戠殑搴忓垪鍖栧拰鍙嶅簭鍒楀寲銆?/span>

        馃檱搴忓垪鍖栨槸灏嗕竴涓暟鎹粨鏋勬垨鑰呭璞¤浆鎹负杩炵画鐨勬瘮鐗逛綅鐨勬搷浣滐紝杩涜€屽彲浠ュ皢杞崲鍚庣殑鏁版嵁瀛樺偍鍦ㄤ竴涓枃浠舵垨鑰呭唴瀛樹腑锛屽悓鏃朵篃鍙互閫氳繃缃戠粶浼犺緭鍒板彟涓€涓绠楁満鐜锛岄噰鍙栫浉鍙嶆柟寮忛噸鏋勫緱鍒板師鏁版嵁銆?/span>

馃檵閫掑綊瑙?/span>娉?/span>

                public String serialize(TreeNode root) { if (root == null) { return "null"; } String rserialize = rserialize(root, ""); return rserialize;}
private String rserialize(TreeNode root, String str) {  if (root == null) { if ("".equals(str)) { str += "null"; } else { str += ",null";      }  } else { if ("".equals(str)) { str += "" + str.valueOf(root.val); } else { str = str + "," + str.valueOf(root.val);      } str = rserialize(root.left, str); str = rserialize(root.right, str); } return str;}
public TreeNode deserialize(String data) {
if (data.equals("null")) { return null; } LinkedList<String> list = new LinkedList<>(Arrays.asList(data.split(","))); return rdeserialize(list);}
private TreeNode rdeserialize(LinkedList<String> list) { if (list.get(0).equals("null")) { list.removeFirst(); return null;  }  TreeNode node = new TreeNode(Integer.valueOf(list.get(0))); list.removeFirst(); node.left = rdeserialize(list); node.right = rdeserialize(list); return node;}

涓嶇Н璺锛屾棤浠ヨ嚦鍗冮噷銆?/strong>

鏂囩珷鏈夊府鍔╃殑璇濓紝鍦ㄧ湅锛岃浆鍙戝惂銆?/strong>

璋㈣阿鏀寔鍝?(*^__^*)

END


浜屽弶鏍戠殑搴忓垪鍖栦笌鍙嶅簭鍒楀寲馃憞


以上是关于浜屽弶鏍戠殑搴忓垪鍖栦笌鍙嶅簭鍒楀寲的主要内容,如果未能解决你的问题,请参考以下文章

92 搴忓垪鍖? 鍙嶅簭鍒楀寲

Dictionary搴忓垪鍖栧拰鍙嶅簭鍒楀寲

[LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 楠岃瘉浜屽弶鎼滅储鏍戠殑鍏堝簭搴忓垪

leetcode鍒烽-94浜屽弶鏍戠殑涓簭閬嶅巻

c璇█ 浜屽弶鏍戠殑鍒涘缓鍙婂叾閫掑綊涓庨潪閫掑綊绠楁硶