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

LinkedIn senior 电面

🔗
laonong15 2016-5-20 10:40:11 | 只看该作者
全局:
s442519769 发表于 2016-5-20 07:06
先求宽度 O(N), 假设root 的x 值为0,
那求最左侧node的x值
有了最左侧的值,之后迭代x值,对每个no ...

感觉不太正确  你上代码看看
回复

使用道具 举报

全局:
下个月 L家 店面!! 楼主说的很详细! 感谢
回复

使用道具 举报

🔗
floyo 2016-8-14 11:00:00 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
sabrinalanlan 2016-10-4 02:01:40 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
howeflguap 2016-10-18 13:12:15 | 只看该作者
全局:
请问楼主第二题的佛楼啊普中,打印时如何处理在某些层存在的某些节点占用同一个位置的重合问题呢(相邻两个父节点,左父的右儿子与右父的左儿子)?直观感觉多层以后相近的父节点位置整数除以二会因为进位而无法避免重合的冲突。
回复

使用道具 举报

🔗
jy_121 2017-10-21 13:51:37 | 只看该作者
全局:
follow up 是间距输出应该怎样做呢?
回复

使用道具 举报

🔗
xiaotdl 2018-3-9 16:18:44 | 只看该作者
全局:
思路跟把整个binary tree inorder存到array里很像。
先dfs求树的高度h。
然后把每层都存到大小为2^h - 1的array里,逐层打出来就好了。
  1. class Solution {
  2. /**
  3. * Sample input:
  4. *
  5. *          1
  6. *         / \
  7. *        2   3
  8. *       / \  / \
  9. *      4   5 6  7
  10. *
  11. * Expected output:
  12. *    1
  13. *    2 3
  14. *    4 5 6 7
  15. *
  16. * Follow up expected output:
  17. *      1   
  18. *   2    3
  19. * 4  5 6 7
  20. **/
  21.     public void levelOrderPrint(TreeNode root) {
  22.         int h = dfs(root);
  23.         int totalNodes = (1 << h) - 1;

  24.         Map<TreeNode, int[]> m = new HashMap<>(); // node2(l,r)

  25.         Queue<TreeNode> q = new LinkedList<>();
  26.         q.offer(root);
  27.         m.put(root, new int[]{0, totalNodes - 1});
  28.         int currH = h;
  29.         while (!q.isEmpty()) {
  30.             int size = q.size();
  31.             int[] lvl = new int[totalNodes];
  32.             for (int i = 0; i < size; i++) {
  33.                 TreeNode curr = q.poll();
  34.                 int currPos = (m.get(curr)[1] + m.get(curr)[0]) / 2;
  35.                 lvl[currPos] = curr.val;
  36.                 if (curr.left != null) {
  37.                     q.offer(curr.left);
  38.                     m.put(curr.left, new int[]{m.get(curr)[0], currPos - 1});
  39.                 }
  40.                 if (curr.right != null) {
  41.                     q.offer(curr.right);
  42.                     m.put(curr.right, new int[]{currPos + 1, m.get(curr)[1]});
  43.                 }
  44.             }
  45.             currH--;
  46.             for (int i = 0; i < lvl.length; i++) {
  47.                 System.out.print(lvl[i] != 0 ? lvl[i] : " ");
  48.             }
  49.             System.out.println();
  50.         }
  51.     }

  52.     private int dfs(TreeNode root) {
  53.         if (root == null) return 0;
  54.         return Math.max(dfs(root.left), dfs(root.right)) + 1;
  55.     }

  56.     public static void main(String[] args) {
  57.         TreeNode _1 = new TreeNode(1);
  58.         TreeNode _2 = new TreeNode(2);
  59.         TreeNode _3 = new TreeNode(3);
  60.         _1.left = _2;
  61.         _1.right = _3;
  62.         TreeNode _4 = new TreeNode(4);
  63.         TreeNode _5 = new TreeNode(5);
  64.         _2.left = _4;
  65.         _2.right = _5;
  66.         TreeNode _6 = new TreeNode(6);
  67.         TreeNode _7 = new TreeNode(7);
  68.         _3.left = _6;
  69.         _3.right = _7;
  70.         new Solution().levelOrderPrint(_1);
  71.     }

  72. }
复制代码
回复

使用道具 举报

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

本版积分规则

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