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

[树/链表/图] 117_PopulatingNextRightPointersinEachNodeII记不住标准答案怎么办

全局:

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

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

x
  1.     public void connect(TreeLinkNode root) {
  2.         TreeLinkNode head = null;
  3.         TreeLinkNode pre = null;
  4.         TreeLinkNode cur = root;
  5.         while (cur != null) {
  6.             while (cur != null) {
  7.                 if (cur.left != null) {
  8.                     if (pre != null) {
  9.                         pre.next = cur.left;
  10.                     } else {
  11.                         head = cur.left;
  12.                     }
  13.                     pre = cur.left;
  14.                 }
  15.                 if (cur.right != null) {
  16.                     if (pre != null) {
  17.                         pre.next = cur.right;
  18.                     } else {
  19.                         head = cur.left;
  20.                     }
  21.                     pre = cur.right;
  22.                 }
  23.                 cur = cur.next;
  24.             }
  25.             cur = head;
  26.             pre = null;
  27.             head = null;
  28.         }
  29.     }
复制代码



貌似还是高频,三个指针来回弄实在记不住,又没cleaner一点的答案

上一篇:说个 刷题过程有意思的 心理波动
下一篇:刷题心得
🔗
yanjinbin 2019-9-11 20:58:17 | 只看该作者
全局:
画图  实在记不住 就放弃 23333 自己 推演一遍  从来不考记住   除了树的各种遍历  和排序 确实需要默写熟练之外  但是也不是先记住 是先理解了  画图 理解了 默写
回复

使用道具 举报

🔗
隔壁老汪 2019-9-12 01:20:56 | 只看该作者
全局:
记不住说明不理解,再多思考思考
回复

使用道具 举报

🔗
337845818 2019-9-12 03:31:48 | 只看该作者
全局:
啥叫标准答案啊..

你解题思路没有, 就不会做. 你思路呢?
回复

使用道具 举报

🔗
magicsets 2019-9-12 15:36:02 | 只看该作者
全局:
楼主看看这个写法是不是比较容易理解一点.. 虽然代码量稍微多了一点,但 "TreeLevel" 和 "forEachNodeInNextLevel" 这两个用于解耦关键逻辑的模块写出来的话在面试时应该会有相当的加分效果

  1. // 用于构造树的一层
  2. class TreeLevel {
  3.     private Node head = null;
  4.     private Node tail = null;

  5.     public void add(Node node) {
  6.         if (head == null) {
  7.             tail = head = node;
  8.         } else {
  9.             tail = tail.next = node;
  10.         }
  11.     }

  12.     public Node getHead() {
  13.         return head;
  14.     }
  15. }


  16. class Solution {
  17.     public Node connect(Node root) {
  18.         for (Node levelStart = root; levelStart != null;) {
  19.             TreeLevel nextLevel = new TreeLevel();
  20.             // 遍历下一层所有节点并加入到'nextLevel'里面
  21.             forEachNodeInNextLevel(levelStart, (node) - > nextLevel.add(node));
  22.             // 进入下一层
  23.             levelStart = nextLevel.getHead();
  24.         }
  25.         return root;
  26.     }

  27.     // 给定当前层的起始节点,遍历下一层的所有节点,并使用回调函数'callback'来处理。
  28.     private void forEachNodeInNextLevel(Node levelStart, Consumer < Node > callback) {
  29.         for (Node node = levelStart; node != null; node = node.next) {
  30.             if (node.left != null) callback.accept(node.left);
  31.             if (node.right != null) callback.accept(node.right);
  32.         };
  33.         // 一层的最后一个节点后面是null
  34.         callback.accept(null);
  35.     }
复制代码
回复

使用道具 举报

🔗
DummyOnlineId 2019-9-15 08:05:56 | 只看该作者
全局:
我前两天刚被考到没有很快的得到O(1) space的解QAQ。。。然后就凉凉了orz 现在大概记一辈子吧。。。。
回复

使用道具 举报

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

本版积分规则

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