查看: 3111| 回复: 7
跳转到指定楼层
上一主题 下一主题
收起左侧

[树/链表/图] Leetcode 297/449 Serialize and Deserialize Binary Tree区别

🔗
pxu | 只看该作者 |倒序浏览
全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
今天做449,发现做法好像跟297一模一样。但感觉应该是需要用到BST的特征才是的。谁可以帮忙解释一下?谢谢!
Leetcode297: Serialize and Deserialize Binary TreeLeetcode 449 Serialize and Deserialize Binary Search Tree

上一篇:关于java中pass int和int[]的问题
下一篇:如何记忆刷过的题目
全局:
你也是一个不爱寻找答案的人吗?

http://www.1point3acres.com/bbs/thread-433802-1-1.html

评分

参与人数 2大米 +8 收起 理由
heyyyyyy + 3 给你点个赞!
pxu + 5 谢谢,搜索要花大米的呀:)

查看全部评分

回复

使用道具 举报

🔗
heyyyyyy 2018-7-28 16:19:44 | 只看该作者
全局:
肥宅快乐水 发表于 2018-7-28 02:01
你也是一个不爱寻找答案的人吗?

http://www.1point3acres.com/bbs/thread-433802-1-1.html

优秀!很优秀!
回复

使用道具 举报

全局:
不是, 我意思你在lc网站上也没怎么看呐(╬ ̄皿 ̄)=○#( ̄#)3 ̄) {:8_247:}
回复

使用道具 举报

🔗
vtiaocao 2018-7-29 02:38:40 | 只看该作者
全局:
肥宅快乐水 发表于 2018-7-28 06:08
不是, 我意思你在lc网站上也没怎么看呐(╬ ̄皿 ̄)=○#( ̄#)3 ̄)

肥宅姐姐真是个热心人



是不是也助长了……
没有,什么都没有助长,助长了刷题风气。
回复

使用道具 举报

全局:
vtiaocao 发表于 2018-7-29 02:38
肥宅姐姐真是个热心人

我男的啊。{:8_247:}

助长了什么。。?{:8_247:}{:8_247:}
回复

使用道具 举报

🔗
 楼主| pxu 2018-7-29 05:42:21 | 只看该作者
全局:
老大们,我这是刷题帖,不是灌水帖哟
回复

使用道具 举报

🔗
 楼主| pxu 2018-7-29 06:30:37 | 只看该作者
全局:

总结一下吧。
两种实现的时间和空间复杂度都是O(N), 所以考点不是这个。考点可能是在面试过程中,面试官可能会用 449 作为follow up.
普通做法:没有用的BST 特征
  1. public class Codec {
  2.     private final static char SP=',';
  3.     private final static char EMPTY='#';
  4.     // Encodes a tree to a single string.
  5.     public String serialize(TreeNode root) {
  6.         StringBuilder sb= new StringBuilder();
  7.         if(root == null) return "";
  8.         Stack<TreeNode> stack = new Stack<>();
  9.         stack.push(root);
  10.         while(!stack.isEmpty()){
  11.             TreeNode curr = stack.pop();
  12.             if(curr == null){
  13.                 sb.append(EMPTY);
  14.             }else{
  15.                 sb.append(curr.val);

  16.                 stack.push(curr.right);
  17.                 stack.push(curr.left);
  18.             }
  19.             
  20.             sb.append(SP);
  21.         }
  22.         
  23.         sb.deleteCharAt(sb.length()-1);
  24.         return sb.toString();
  25.     }

  26.     // Decodes your encoded data to tree.
  27.     public TreeNode deserialize(String data) {
  28.         if(data == null || data.length() == 0) return null;
  29.         String array[] = data.split("\\" + SP);
  30.         Queue<String> queue = new LinkedList<>();
  31.         for(String val: array){
  32.             queue.add(val);
  33.         }
  34.         
  35.         return deserializeHelper(queue);
  36.     }
  37.    
  38.     public TreeNode deserializeHelper(Queue<String> queue){
  39.         if(queue.isEmpty()) return null;
  40.         String valStr = queue.remove();
  41.         if(valStr.equals("" + EMPTY)) return null;
  42.         int val = Integer.valueOf(valStr);
  43.         TreeNode root = new TreeNode(val);
  44.         root.left = deserializeHelper(queue);
  45.         root.right = deserializeHelper(queue);
  46.         
  47.         return root;
  48.         
  49.     }
  50. }
复制代码



用的BST 拍好序的特征:代码相对简洁一些,不需要存空叶子的值,而是利用preOrder和inorder的性质去实现
  1. public class Codec {
  2.     private final static char SP=',';
  3.     // Encodes a tree to a single string.
  4.     public String serialize(TreeNode root) {
  5.         StringBuilder sb= new StringBuilder();
  6.         if(root == null) return "";
  7.         Stack<TreeNode> stack = new Stack<>();
  8.         stack.push(root);
  9.         while(!stack.isEmpty()){
  10.             TreeNode curr = stack.pop();
  11.             sb.append(curr.val).append(SP);
  12.             if(curr.right!=null) stack.push(curr.right);
  13.             if(curr.left !=null) stack.push(curr.left);
  14.             
  15.         }
  16.         
  17.         sb.deleteCharAt(sb.length()-1);
  18.         return sb.toString();
  19.     }

  20.     // Decodes your encoded data to tree.
  21.     public TreeNode deserialize(String data) {
  22.         if(data == null || data.length() == 0) return null;
  23.         String array[] = data.split("\\" + SP);
  24.         Queue<Integer> queue = new LinkedList<>();
  25.         for(String val: array){
  26.             queue.add(Integer.valueOf(val));
  27.         }
  28.         
  29.         return deserializeHelper(queue);
  30.     }
  31.    
  32.     public TreeNode deserializeHelper(Queue<Integer> queue){
  33.         if(queue.isEmpty()) return null;
  34.         Integer curr = queue.remove();
  35.    
  36.         Queue<Integer> smallerQueue = new LinkedList<>();
  37.         while(!queue.isEmpty() && queue.peek() < curr){
  38.             smallerQueue.add(queue.remove());
  39.         }
  40.         
  41.         TreeNode root = new TreeNode(curr);
  42.         root.left = deserializeHelper(smallerQueue);
  43.         root.right = deserializeHelper(queue);
  44.         
  45.         return root;
  46.         
  47.     }
  48. }
复制代码





评分

参与人数 1大米 +10 收起 理由
carry12345678 + 10 很有用的信息!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表