当前位置: 首页 > news >正文

LeetCode-0525

102. 二叉树的层序遍历(中等)

思路:使用hash记录深度

class Solution {public List<List<Integer>> levelOrder(TreeNode root) {if(root==null)return new ArrayList<>();Map<TreeNode,Integer> deep = new HashMap<>();Queue<TreeNode> queue = new LinkedList<>();int d = deepth(root);List<List<Integer>> res = new ArrayList<>();for(int i=0;i<d;i++){res.add(new ArrayList<>());}queue.offer(root);deep.put(root,0);while(!queue.isEmpty()){TreeNode cur = queue.poll();int dp = deep.get(cur);res.get(dp).add(cur.val);if(cur.left!=null){queue.offer(cur.left);deep.put(cur.left,dp+1);}if(cur.right!=null){queue.offer(cur.right);deep.put(cur.right,dp+1);}}return res;}public int deepth(TreeNode root){if(root==null)return 0;return Math.max(deepth(root.left),deepth(root.right))+1;}
}

107. 二叉树的层序遍历 II(中等)

思路:相比刚才的解答,只需要将数组的赋值改一下即可

class Solution {public List<List<Integer>> levelOrderBottom(TreeNode root) {if(root==null)return new ArrayList<>();Map<TreeNode,Integer> deep = new HashMap<>();Queue<TreeNode> queue = new LinkedList<>();int d = deepth(root);List<List<Integer>> res = new ArrayList<>();for(int i=0;i<d;i++){res.add(new ArrayList<>());}queue.offer(root);deep.put(root,0);while(!queue.isEmpty()){TreeNode cur = queue.poll();int dp = deep.get(cur);res.get(d-dp-1).add(cur.val);if(cur.left!=null){queue.offer(cur.left);deep.put(cur.left,dp+1);}if(cur.right!=null){queue.offer(cur.right);deep.put(cur.right,dp+1);}}return res;}public int deepth(TreeNode root){if(root==null)return 0;return Math.max(deepth(root.left),deepth(root.right))+1;}
}

199. 二叉树的右视图(中等)

思路:一开始想一直找最右子,然后发现有可能左子树比右子树高,这样就不对了。之后还是在层序遍历的模版上微调一下

class Solution {public List<Integer> rightSideView(TreeNode root) {if(root==null)return new ArrayList<>();Map<TreeNode,Integer> deep = new HashMap<>();Queue<TreeNode> queue = new LinkedList<>();int d = deepth(root);List<List<Integer>> res = new ArrayList<>();List<Integer> ans = new ArrayList<>();for(int i=0;i<d;i++){res.add(new ArrayList<>());}queue.offer(root);deep.put(root,0);while(!queue.isEmpty()){TreeNode cur = queue.poll();int dp = deep.get(cur);res.get(dp).add(cur.val);if(cur.left!=null){queue.offer(cur.left);deep.put(cur.left,dp+1);}if(cur.right!=null){queue.offer(cur.right);deep.put(cur.right,dp+1);}}for(int i=0;i<d;i++){List<Integer> tp = res.get(i);ans.add(tp.get(tp.size()-1));}return ans;}public int deepth(TreeNode root){if(root==null)return 0;return Math.max(deepth(root.left),deepth(root.right))+1;}
}

637. 二叉树的层平均值(简单)

思路:还是之前的模版

官方题解里面,可以在while里面for遍历队列进行处理,每一次遍历,都是同一层的元素,极好的解决了深度问题

class Solution {public List<Double> averageOfLevels(TreeNode root) {if(root==null)return new ArrayList<>();Map<TreeNode,Integer> deep = new HashMap<>();Queue<TreeNode> queue = new LinkedList<>();int d = deepth(root);List<List<Integer>> res = new ArrayList<>();List<Double> ans = new ArrayList<>();for(int i=0;i<d;i++){res.add(new ArrayList<>());}queue.offer(root);deep.put(root,0);while(!queue.isEmpty()){TreeNode cur = queue.poll();int dp = deep.get(cur);res.get(dp).add(cur.val);if(cur.left!=null){queue.offer(cur.left);deep.put(cur.left,dp+1);}if(cur.right!=null){queue.offer(cur.right);deep.put(cur.right,dp+1);}}for(int i=0;i<d;i++){List<Integer> tp = res.get(i);double sum = 0;for(int j=0;j<tp.size();j++){sum+=tp.get(j);}ans.add(sum/tp.size());}return ans;}public int deepth(TreeNode root){if(root==null)return 0;return Math.max(deepth(root.left),deepth(root.right))+1;}
}

429. N 叉树的层序遍历(中等)

思路:使用上一题官方题解的办法

class Solution {public List<List<Integer>> levelOrder(Node root) {List<List<Integer>> res  = new ArrayList<>();if(root == null) return res;Queue<Node> queue = new LinkedList<>();queue.add(root);while(!queue.isEmpty()){List<Integer> level = new ArrayList<>();int size = queue.size();	//	这里一定要先取出size,不然中间会增加for(int i=0;i<size;i++){Node cur = queue.poll();level.add(cur.val);for(int j=0;j<cur.children.size();j++){queue.offer(cur.children.get(j));}}res.add(level);}return res;}
}

515. 在每个树行中找最大值(中等)

class Solution {public List<Integer> largestValues(TreeNode root) {List<Integer> res  = new ArrayList<>();if(root == null) return res;Queue<TreeNode> queue = new LinkedList<>();queue.add(root);while(!queue.isEmpty()){int max = queue.peek().val;int size = queue.size();for(int i=0;i<size;i++){TreeNode cur = queue.poll();if(cur.val>max)max = cur.val;if(cur.left!=null)queue.offer(cur.left);if(cur.right!=null)queue.offer(cur.right);}res.add(max);}return res;}
}

116. 填充每个节点的下一个右侧节点指针

class Solution {public Node connect(Node root) {if(root==null)return root;Queue<Node> queue = new LinkedList<>();queue.add(root);while(!queue.isEmpty()){int size = queue.size();Node last = null;for(int i=0;i<size;i++){Node cur = queue.poll();if(last!=null)last.next = cur;last = cur;if(cur.left!=null)queue.offer(cur.left);if(cur.right!=null)queue.offer(cur.right);}}return root;}
}

117. 填充每个节点的下一个右侧节点指针 II(中等)

完全同上

104. 二叉树的最大深度(简单)

class Solution {public int maxDepth(TreeNode root) {if(root==null)return 0;return Math.max(maxDepth(root.left),maxDepth(root.right))+1;}
}

111. 二叉树的最小深度(简单)

class Solution {public int minDepth(TreeNode root) {if(root==null)return 0;if(root.left==null||root.right==null)return Math.max(minDepth(root.left),minDepth(root.right))+1;return Math.min(minDepth(root.left),minDepth(root.right))+1;}}
http://www.lryc.cn/news/89872.html

相关文章:

  • 【Linux 】scp命令
  • Docker部署yolov5
  • 如何在 Axios 中去控制 Loading?大有学问!
  • 充电桩检测设备厂家TK4860C交流充电桩检定装置
  • 一文3000字实现基于Selenium+Python的web自动化测试框架
  • Android 12系统源码_窗口管理(二)WindowManager对窗口的管理过程
  • python3.8,torch1.10.2+cu113、torch-geometric 安装
  • 堆(heap)、栈(stack)
  • 企业级API网关之典型应用场景
  • 【2023年4月美赛加赛】Z题:The future of Olympics 25页完整论文
  • Rocket重试机制,消息模式,刷盘方式
  • linux+onenet可视化(图形化步骤)
  • 汇编的基础
  • 并发编程学习(十四):tomcat线程池
  • 简洁灵活工单管理系统,支持工单模版字段、工单状态自定义
  • 标签派单系统架构设计
  • Jmeter和Postman那个工具更适合做接口测试?
  • k8s污点与容忍
  • 市面上有哪些软件可以结合agentgpt的?众包平台结合的好处!
  • 【js】对象属性的拦截和Proxy代理与Reflect映射的用法与区别
  • Yolov8涨点神器:ODConv+ConvNeXt提升小目标检测能力
  • git代码回滚是使用reset还是revert
  • 深入理解Java ThreadLocal及其内存泄漏防范
  • 介绍10款ChatGPT替代产品
  • 数字逻辑 期末
  • MT4交易外汇平台有哪些优势?为何是外汇投资首选?
  • 问卷调查工具实力榜单发布
  • javascript中property和attribute有什么区别?
  • 快速上手kettle
  • Leetcode 399. 除法求值