中级农民
- 积分
- 225
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2021-11-29
- 最后登录
- 1970-1-1
|
注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
如题
一头雾水
我这个code ,314 可以跑过 , 987就down 掉了- public List<List<Integer>> verticalOrder(TreeNode root) {
- List<List<Integer>> res = new ArrayList<>();
-
- if(root==null)
- return res;
-
- Map<Integer, List<Integer>> map = new TreeMap<>();
- Queue<TreeNode> nodeQ = new LinkedList<>();
- Queue<Integer> offsetQ = new LinkedList<>();
- nodeQ.offer(root);
- offsetQ.offer(0);
- int maxVal=0,minVal=0;
-
- while(!nodeQ.isEmpty() ){
- TreeNode curNode = nodeQ.poll();
- int curOffset = offsetQ.poll();
- maxVal=Math.max(maxVal, curOffset);
- minVal=Math.min(minVal, curOffset);
- map.computeIfAbsent(curOffset,x->new ArrayList<>() ).add(curNode.val);
- if(curNode.left != null){
- nodeQ.offer(curNode.left);
- offsetQ.offer(curOffset-1);
- }
- if(curNode.right != null){
- nodeQ.offer(curNode.right);
- offsetQ.offer(curOffset+1);
- }
- }
- for(int i=minVal;i<=maxVal;i++)
- res.add(map.get(i));
- return res;
- }
复制代码 |
上一篇: 力口刷题详细题解(详细思路+一题多解)下一篇: 大家刷题都用什么工具总结
|