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

每日5题Day22 - LeetCode 106 - 110

每一步向前都是向自己的梦想更近一步,坚持不懈,勇往直前!

第一题:106. 从中序与后序遍历序列构造二叉树 - 力扣(LeetCode)

class Solution {public TreeNode buildTree(int[] inorder, int[] postorder) {Map<Integer, Integer> map = new HashMap<>();//把中序每个点都记录下来for(int i = 0; i < inorder.length; i++){map.put(inorder[i], i);}//传入后序的最后一个节点,因为是根节点TreeNode head = helper(0, inorder.length - 1, map, postorder, postorder.length - 1);return head;}private static TreeNode helper(int low, int high, Map<Integer, Integer> map, int[] postorder, int idx){if(low > high){return null;}//找到对应的值int val = postorder[idx];//在中序中找到分割点int index = map.get(val);TreeNode node = new TreeNode(val);//递归找到左右子树node.left = helper(low, index - 1, map, postorder, idx - (high - index) - 1);node.right = helper(index + 1, high, map, postorder, idx - 1);return node;}
}

第二题:107. 二叉树的层序遍历 II - 力扣(LeetCode)

class Solution {public List<List<Integer>> levelOrderBottom(TreeNode root) {//就是使用栈每次把每一层记录下来,最后逆序输出//本质上还是属于层序遍历List<List<Integer>> res = new ArrayList<>();if(root == null){return res;}Deque<TreeNode> stack = new ArrayDeque<>();stack.offer(root);while(!stack.isEmpty()){List<Integer> tmp = new ArrayList<>();int size = stack.size();for(int i = 0; i < size; i++){TreeNode cur_node = stack.poll();tmp.add(cur_node.val);if(cur_node.left != null){stack.offer(cur_node.left);}if(cur_node.right != null){stack.offer(cur_node.right);}}res.add(0,tmp);}return res;}
}

第三题:108. 将有序数组转换为二叉搜索树 - 力扣(LeetCode)

class Solution {public TreeNode sortedArrayToBST(int[] nums) {//题目提到有序,二叉,高度要平衡,//相当于每次我们找到节点都是要中间节点//随后向两边扩散,所以创建二叉搜索树的时候一定要二叉return helper(nums, 0 , nums.length - 1);}private TreeNode helper(int[] nums, int left, int right){if(left > right){return null;}//找到中间节点int mid = left + (right - left) / 2;TreeNode node = new TreeNode(nums[mid]);//递归node.left = helper(nums, left, mid - 1);node.right = helper(nums, mid + 1, right);//注意是返回某个节点return node;}
}

第四题:109. 有序链表转换二叉搜索树 - 力扣(LeetCode)

class Solution {public TreeNode sortedListToBST(ListNode head) {if (head == null) {return null; // 基本情况}return convertToBST(head, null);}private TreeNode convertToBST(ListNode head, ListNode tail) {if (head == tail) {return null; // 基本情况:子列表为空}//使用快慢指针来实现二分中查找中位的过程ListNode slow = head;ListNode fast = head;while (fast != tail && fast.next != tail) {slow = slow.next;fast = fast.next.next;}TreeNode root = new TreeNode(slow.val);root.left = convertToBST(head, slow);root.right = convertToBST(slow.next, tail);return root;}
}

 第五题:110. 平衡二叉树 - 力扣(LeetCode)

class Solution {public boolean isBalanced(TreeNode root) {//对于每一个点进行递归判断return getH(root) != -1;}private static int getH(TreeNode node){//对于每一个特定的点,如果为空则高度为0if(node == null){return 0;}//找到左边,如果左边不满足,则自身也不满足int left = getH(node.left);if(left == -1){return -1;}//同理,右边同样逻辑int right = getH(node.right);if(right == -1){return -1;}//注意下面判别式,判断左右高度差值是否大于1//大了肯定不满足了//否则我们给出一个最大高度if(Math.abs(left - right) > 1){return -1;}else{return Math.max(left, right) + 1;}}
}

http://www.lryc.cn/news/370477.html

相关文章:

  • 【Python】读取文件夹中所有excel文件拼接成一个excel表格 的方法
  • 7. 通配符和正则表达式
  • ROS2底层机制源码分析
  • 超越 Transformer开启高效开放语言模型的新篇章
  • 快速排序-Hoare 递归版 C语言
  • C语言经典指针运算笔试题图文解析
  • 使用 KubeKey v3.1.1 离线部署原生 Kubernetes v1.28.8 实战
  • DOS 命令
  • 如何用Java程序实现一个简单的消息队列?
  • OpenAI 宕机事件:GPT 停摆的影响与应对
  • linux常用的基础命令
  • 618家用智能投影仪推荐:这个高性价比品牌不容错过
  • 自愿离婚协议书
  • WPS JSA 宏脚本入门和样例
  • Printing and Exporting
  • c++【入门】正多边形每个内角的度数
  • spring boot3登录开发-邮箱登录/注册接口实现
  • 数据结构-二叉搜索树
  • JUnit:Java开发者不可或缺的单元测试框架
  • NG32单片机GPIO口配置方式
  • SpringCloud-OpenFeign拓展-连接池、最佳使用方法、日志输出
  • 跨链协议中Cosmos IBC、Polkadot/XCM、Celer Network的区别以及用途
  • 电子画册制作与传统画册相比,有哪些优势?
  • postman如何导入证书
  • RocketMQ教程(八):RocketMQ的集群搭建
  • 线上观看人次2万+!「飞天技术沙龙-CentOS 迁移替换专场」北京站圆满结束
  • Docker基本架构概览-1
  • OZON云仓靠谱吗,OZON云仓垫资提货模式
  • 数据集笔记:DGraph 大规模动态图数据集
  • 一些常用的git指令总结