123
返回列表 发新帖
楼主: cgdong2012
跳转到指定楼层
上一主题 下一主题
收起左侧

Fackbook 下午刚电面的面筋

🔗
tsingpg 2016-2-21 00:22:13 | 只看该作者
全局:
yanggao1119 发表于 2016-2-19 09:52
个人感觉,楼上解法mix了recursive solution,和iterative solution,不够简明。

我的iterative solutio ...

Hi 你这个里面其实最后prev和tail指向的是同一个node吧,其实就是可以省略tail节点的
回复

使用道具 举报

🔗
yanggao1119 2016-2-21 02:36:17 | 只看该作者
全局:
tsingpg 发表于 2016-2-21 00:22
Hi 你这个里面其实最后prev和tail指向的是同一个node吧,其实就是可以省略tail节点的

赞! great suggestion!我改了下,为了语义考虑,用head和tail:

        public TreeNode BT2CircularDLL(TreeNode root) {
                if (root == null) {
                        return root;
                }
                Stack<TreeNode> st = new Stack<>();
                TreeNode curr = root;
                TreeNode head = null;
                TreeNode tail = null;
                while (curr != null || !st.isEmpty()) {
                        if (curr != null) {
                                st.push(curr);
                                curr = curr.left;
                        } else {
                                TreeNode popped = st.pop();
                                if (tail != null) {
                                        tail.right = popped;
                                        popped.left = tail;
                                } else {
                                        head = popped;
                                }
                                tail = popped;
                                curr = popped.right;
                        }
                }
                tail.right = head;
                head.left = tail;
                return head;
        }
回复

使用道具 举报

🔗
fame 2016-2-23 01:53:42 | 只看该作者
全局:
答得不错,楼主加油!早日拿到offer
回复

使用道具 举报

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

本版积分规则

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