序列化二叉树 Posted on 2019-08-13 | | reads times 序列化二叉树题目描述 请实现两个函数,分别用来序列化和反序列化二叉树 利用数组实现序列化结果,重建过程相当于建树,用前序结果建树。 递归 123456789101112131415161718192021222324252627282930313233function TreeNode(x) { this.val = x; this.left = null; this.right = null;} const arr=[]function Serialize(pRoot){ // write code here if(pRoot==null){ arr.push('#') }else{ arr.push(pRoot.val) Serialize(pRoot.left) Serialize(pRoot.right) }}function Deserialize(){ // write code here let node=null; if(arr.length<1){ return null; } let number=arr.shift() if(typeof(number)=='number'){ node=new TreeNode(number) node.left=Deserialize(); node.right=Deserialize() } return node;} Post author: GoldMiner Xun Post link: https://goldminerxun.github.io/2019/08/13/%E5%89%91%E6%8C%87offer%20JavaScript%E7%89%88%20(61)/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.