算法练习(2):牛客在线编程03 二叉树
package jz.bm;import jz.TreeNode;import java.util.*;public class bm3 {/*** BM23 二叉树的前序遍历*/public int[] preorderTraversal (TreeNode root) {ArrayList<Integer> list = new ArrayList<>();preOrder(root, list);int[] res = new int[list.size()];for (int i = 0; i < list.size(); i++) {res[i] = list.get(i);}return res;}private void preOrder(TreeNode root, ArrayList<Integer> list) {if (root != null) {list.add(root.val);preOrder(root.left, list);preOrder(root.right, list);}}/*** BM24 二叉树的中序遍历*/public int[] inorderTraversal (TreeNode root) {ArrayList<Integer> list = new ArrayList<>();inOrder(root, list);int[] res = new int[list.size()];for (int i = 0; i < list.size(); i++) {res[i] = list.get(i);}return res;}private void inOrder(TreeNode root, ArrayList<Integer> list) {if (root != null) {inOrder(root.left, list);list.add(root.val);inOrder(root.right, list);}}/*** BM25 二叉树的后序遍历*/public int[] postorderTraversal (TreeNode root) {ArrayList<Integer> list = new ArrayList<>();postOrder(root, list);int[] res = new int[list.size()];for (int i = 0; i < list.size(); i++) {res[i] = list.get(i);}return res;}private void postOrder(TreeNode root, ArrayList<Integer> list) {if (root != null) {postOrder(root.left, list);postOrder(root.right, list);list.add(root.val);}}/*** BM26 求二叉树的层序遍历*/public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {ArrayList<ArrayList<Integer>> res = new ArrayList<>();if (root == null) {return res;}Queue<TreeNode> queue = new LinkedList<>();queue.add(root);while (!queue.isEmpty()) {int n = queue.size();ArrayList<Integer> list = new ArrayList<>();for (int i = 0; i < n; i++) {TreeNode cur = queue.poll();list.add(cur.val);if (cur.left != null) {queue.add(cur.left);}if (cur.right != null) {queue.add(cur.right);}}res.add(list);}return res;}/*** BM27 按之字形顺序打印二叉树*/public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {ArrayList<ArrayList<Integer>> res = new ArrayList<>();if (pRoot == null) {return res;}Queue<TreeNode> queue = new LinkedList<>();queue.add(pRoot);boolean flag = false;while (!queue.isEmpty()) {int n = queue.size();ArrayList<Integer> list = new ArrayList<>();for (int i = 0; i < n; i++) {TreeNode cur = queue.poll();list.add(cur.val);if (cur.left != null) {queue.add(cur.left);}if (cur.right != null) {queue.add(cur.right);}}if (flag) {Collections.reverse(list);}flag = !flag;res.add(list);}return res;}/*** BM28 二叉树的最大深度*/int max = 0;public int maxDepth (TreeNode root) {dfs28(root, 0);return max;}private void dfs28(TreeNode root, int depth) {if (root != null) {depth += 1;max = Math.max(max, depth);dfs28(root.left, depth);dfs28(root.right, depth);}}/*** BM29 二叉树中和为某一值的路径(一)*/boolean hasPath = false;public boolean hasPathSum (TreeNode root, int sum) {dfs29(root, sum);return hasPath;}private void dfs29(TreeNode root, int cur) {if (root != null) {cur -= root.val;if (root.left == null && root.right == null && cur == 0) {hasPath = true;return;}dfs29(root.left, cur);dfs29(root.right, cur);}}/*** BM30 二叉搜索树与双向链表*/TreeNode head;TreeNode pre;public TreeNode Convert(TreeNode pRootOfTree) {if (pRootOfTree != null) {Convert(pRootOfTree.left);if (pre == null) {head = pRootOfTree;} else {pre.right = pRootOfTree;pRootOfTree.left = pre;}pre = pRootOfTree;Convert(pRootOfTree.right);}return head;}/*** BM31 对称的二叉树*/public boolean isSymmetrical (TreeNode pRoot) {if (pRoot != null) {return mirror(pRoot.left, pRoot.right);} else {return true;}}private boolean mirror(TreeNode left, TreeNode right) {if (left == null && right == null) {return true;}if (right == null || left == null || left.val != right.val) {return false;}return mirror(left.left, right.right) && mirror(left.right, right.left);}/*** BM32 合并二叉树*/public TreeNode mergeTrees (TreeNode t1, TreeNode t2) {if (t1 == null) {return t2;}if (t2 == null) {return t1;}t1.val = t1.val + t2.val;t1.left = mergeTrees(t1.left, t2.left);t1.right = mergeTrees(t1.right, t2.right);return t1;}/*** BM34 判断是不是二叉搜索树*/boolean res34 = true;TreeNode pre34 = null;public boolean isValidBST (TreeNode root) {inOrder(root);return res34;}private void inOrder(TreeNode root) {if (root != null) {inOrder(root.left);if (pre34 == null) {pre34 = root;} else {if (root.val <= pre34.val) {res34 = false;}}pre34 = root;inOrder(root.right);}}/*** BM35 判断是不是完全二叉树*/public boolean isCompleteTree (TreeNode root) {if (root == null) {return true;}Queue<TreeNode> queue = new LinkedList<>();queue.add(root);boolean hasNull = false;while (!queue.isEmpty()) {TreeNode cur = queue.poll();if (cur == null) {hasNull = true; //遇到空的节点} else {//空的节点后面有非空的节点if (hasNull) {return false;}queue.add(cur.left);queue.add(cur.right);}}return true;}/*** BM36 判断是不是平衡二叉树*/public boolean IsBalanced_Solution (TreeNode pRoot) {if (pRoot == null) {return true;}return IsBalanced_Solution(pRoot.left) && IsBalanced_Solution(pRoot.right) && Math.abs(dfs36(pRoot.left) - dfs36(pRoot.right)) <= 1;}private int dfs36(TreeNode pRoot) {if (pRoot != null) {return Math.max(dfs36(pRoot.left), dfs36(pRoot.right)) + 1;}return 0;}/*** BM37 二叉搜索树的最近公共祖先*/public int lowestCommonAncestor (TreeNode root, int p, int q) {if (root == null) {return -1;}if ((p <= root.val && root.val <= q) || (q <= root.val && root.val <= p)) {return root.val;} else if (p <= root.val && q <= root.val) {return lowestCommonAncestor(root.left, p, q);} else {return lowestCommonAncestor(root.right, p, q);}}/*** BM38 在二叉树中找到两个节点的最近公共祖先*/public int lowestCommonAncestor1 (TreeNode root, int o1, int o2) {if (root == null) {return -1;}if (o1 == root.val || o2 == root.val) {return root.val;}int left = lowestCommonAncestor1(root.left, o1, o2);int right = lowestCommonAncestor1(root.right, o1, o2);if (left == -1) {return right;}if (right == -1) {return left;}//即不是左也不是右,只能是当前节点return root.val;}/*** BM40 重建二叉树*/public TreeNode reConstructBinaryTree (int[] preOrder, int[] vinOrder) {if (preOrder.length == 0 || vinOrder.length == 0) {return null;}TreeNode head = new TreeNode(preOrder[0]);int index = getIndex(preOrder[0],vinOrder);head.left = reConstructBinaryTree(Arrays.copyOfRange(preOrder, 1, index + 1), Arrays.copyOfRange(vinOrder, 0, index));head.right = reConstructBinaryTree(Arrays.copyOfRange(preOrder, index + 1, preOrder.length), Arrays.copyOfRange(vinOrder, index + 1, vinOrder.length));return head;}private int getIndex(int head, int[] vinOrder) {for (int i = 0; i < vinOrder.length; i++) {if (head == vinOrder[i]) {return i;}}return -1;}/*** BM41 输出二叉树的右视图*/public int[] solve (int[] preOrder, int[] inOrder) {TreeNode head = reConstructBinaryTree(preOrder, inOrder);if (head == null) {return new int[0];}ArrayList<Integer> list = new ArrayList<>();Queue<TreeNode> queue = new LinkedList<>();queue.add(head);while (!queue.isEmpty()) {int n = queue.size();for (int i = 0; i < n; i++) {TreeNode cur = queue.poll();if (i == n - 1) {list.add(cur.val);}if (cur.left != null) {queue.add(cur.left);}if (cur.right != null) {queue.add(cur.right);}}}int[] res = new int[list.size()];for (int i = 0; i < list.size(); i++) {res[i] = list.get(i);}return res;}
}
相关文章:
算法练习(2):牛客在线编程03 二叉树
package jz.bm;import jz.TreeNode;import java.util.*;public class bm3 {/*** BM23 二叉树的前序遍历*/public int[] preorderTraversal (TreeNode root) {ArrayList<Integer> list new ArrayList<>();preOrder(root, list);int[] res new int[list.size()];fo…...

回归预测 | MATLAB实现TCN-BiLSTM时间卷积双向长短期记忆神经网络多输入单输出回归预测
回归预测 | MATLAB实现TCN-BiLSTM时间卷积双向长短期记忆神经网络多输入单输出回归预测 目录 回归预测 | MATLAB实现TCN-BiLSTM时间卷积双向长短期记忆神经网络多输入单输出回归预测预测效果基本介绍模型描述程序设计参考资料 预测效果 基本介绍 1.MATLAB实现TCN-BiLSTM时间卷积…...

Linux 系列 常见 快捷键总结
强制停止 Ctrl C 退出程序、退出登录 Ctrl D 等价 exit 查看历史命令 history !命令前缀,自动匹配上一个命令 (历史命令中:从最新——》最老 搜索) ctrl r 输入内去历史命令中检索 # 回车键可以直接执行 ctrl a 跳到命令开头 …...

OA系统构建排座
目录 一.排座的介绍,作用 1.排座介绍 A.前端实现 B.数据库实现 C.后端实现 2.排座作用 A.座位预订 B.座位安排 C. 实时座位状态显示 二.利用Layui实现排座 1.基础版(通过htmlcssjs实现) A.基础版源码(html): 2.进阶版 …...

微信小程序 居中、居右、居底和横向、纵向布局,文字在图片中间,网格布局
微信小程序居中、居右、横纵布局 1、水平垂直居中(相对父类控件)方式一:水平垂直居中 父类控件: display: flex;align-items: center;//子控件垂直居中justify-content: center;//子控件水平居中width: 100%;height: 400px //注意…...
【C++】总结2
文章目录 1.final和override关键字2.extern "C"的用法3.野指针和垂悬指针(悬空指针)4.指针指向的内存被释放是什么意思5.C和C的类型安全6.C中的重载、重写(覆盖)和隐藏的区别 1.final和override关键字 final和override是C11引入的关键字&…...

vue2项目中使用svg图标
在开发项目的时候经常会用到svg矢量图,而且我们使用SVG以后,页面上加载的不再是图片资源, 这对页面性能来说是个很大的提升,而且我们SVG文件比img要小的很多,放在项目中几乎不占用资源。 1、安装SVG依赖插件并配置加载器和路径 npm instal…...

阿里云盘自动每日签到无需部署无需服务器(仅限学习交流使用)
一、前言 阿里云盘自动每日签到,无需部署,无需服务器 执行思路:使用金山文档的每日定时任务,执行阿里云盘签到接口。 二、效果展示: 三、步骤: 1、进入金山文档网页版 金山文档官网:https:…...

Blazor前后端框架Known-V1.2.7
V1.2.7 Known是基于C#和Blazor开发的前后端分离快速开发框架,开箱即用,跨平台,一处代码,多处运行。 Gitee: https://gitee.com/known/KnownGithub:https://github.com/known/Known 概述 基于C#和Blazor…...

工业边缘计算为什么?
在工厂环境中使用边缘计算并不新鲜。可编程逻辑控制器(PLC)、微控制器、服务器和PC进行本地数据处理,甚至是微型数据中心都是边缘技术,已经在工厂系统中存在了几十年。在车间里看到的看板系统,打卡系统,历史…...

Training-Time-Friendly Network for Real-Time Object Detection 论文学习
1. 解决了什么问题? 目前的目标检测器很少能做到快速训练、快速推理,并同时保持准确率。直觉上,推理越快的检测器应该训练也很快,但大多数的实时检测器反而需要更长的训练时间。准确率高的检测器大致可分为两类:推理时…...
HTTP改HTTPS
tomcat中http协议改https 第一步,配置server.xml <?xml version"1.0" encoding"UTF-8"?> <Server port"8005" shutdown"SHUTDOWN"><Listener className"org.apache.catalina.startup.VersionLogger…...

网络层中一些零碎且易忘的知识点
异构网络:指传输介质、数据编码方式、链路控制协议以及数据单元格式和转发机制不同,异构即物理层和数据链路层均不同RIP、OSPF、BGP分别是哪一层的协议: -RIPOSPFBGP所属层次应用层网络层应用层封装在什么协议中UDPIPTCP 一个主机可以有多个I…...

【RabbitMQ】之高可用集群搭建
目录 一、RabbitMQ 集群原理 1、默认集群原理2、镜像集群原理3、负载均衡方案 二、RabbitMQ 高可用集群搭建 1、RabbitMQ 集群搭建2、配置镜像队列3、HAProxy 环境搭建4、Keepalived 环境搭建 一、RabbitMQ 集群简介 1、默认集群原理 3-1、RabbitMQ 集群简介 单台 RabbitM…...

【node.js】01-fs读写文件内容
目录 一、fs.readFile() 读取文件内容 二、fs.writeFile() 向指定的文件中写入内容 案例:整理txt 需求: 代码: 一、fs.readFile() 读取文件内容 代码: //导入fs模块,从来操作文件 const fs require(fs)// 2.调…...

GitHub仓库如何使用
核心:GitHub仓库如何使用 目录 1.创建仓库: 2.克隆仓库到本地: 3.添加、提交和推送更改: 4.分支管理: 5.拉取请求(Pull Requests): 6.合并代码: 7.其他功能&…...
雪花算法,在分布式环境下实现高效的ID生成
其实雪花算法比较简单,可能称不上什么算法就是一种构造UID的方法。 点1:UID是一个long类型的41位时间戳,10位存储机器码,12位存储序列号。 点2:时间戳的单位是毫秒,可以同时链接1024台机器,每台…...
使用css 动画实现,水波纹的效果
每日鸡汤:每个你想要学习的瞬间都是未来的你向自己求救 需求,实现水波纹动画效果,要求中心一个圆点,然后有3个圈,一圈一圈的向里面缩小。 说实话我第一个想到了给3个圈设置不同的宽高,然后设置动画0-100%&a…...

Unity光照相关知识和实践 (烘焙光照,环境光设置,全局光照)
简介 本文将会通过一个简单的场景搭建,介绍如何使用烘焙光照以及相关的注意事项。另外还介绍了Unity内全局光照(GI)的知识和GI实际在游戏内的表现效果。 Unity关于光照相关的参考文档地址:https://docs.unity.cn/cn/current/Man…...

【设计模式——学习笔记】23种设计模式——适配器模式Adapter(原理讲解+应用场景介绍+案例介绍+Java代码实现)
文章目录 介绍生活中的案例基础介绍工作原理分类应用场景 案例类适配器模式例1介绍类图代码实现优缺点分析 例2类图代码实现 对象适配器模式(常用方式)例1介绍类图代码实现优缺点分析 例2代码实现 接口适配器模式介绍类图代码实现 登场角色类图类适配器模…...
Vim 调用外部命令学习笔记
Vim 外部命令集成完全指南 文章目录 Vim 外部命令集成完全指南核心概念理解命令语法解析语法对比 常用外部命令详解文本排序与去重文本筛选与搜索高级 grep 搜索技巧文本替换与编辑字符处理高级文本处理编程语言处理其他实用命令 范围操作示例指定行范围处理复合命令示例 实用技…...

未来机器人的大脑:如何用神经网络模拟器实现更智能的决策?
编辑:陈萍萍的公主一点人工一点智能 未来机器人的大脑:如何用神经网络模拟器实现更智能的决策?RWM通过双自回归机制有效解决了复合误差、部分可观测性和随机动力学等关键挑战,在不依赖领域特定归纳偏见的条件下实现了卓越的预测准…...

【WiFi帧结构】
文章目录 帧结构MAC头部管理帧 帧结构 Wi-Fi的帧分为三部分组成:MAC头部frame bodyFCS,其中MAC是固定格式的,frame body是可变长度。 MAC头部有frame control,duration,address1,address2,addre…...
JVM暂停(Stop-The-World,STW)的原因分类及对应排查方案
JVM暂停(Stop-The-World,STW)的完整原因分类及对应排查方案,结合JVM运行机制和常见故障场景整理而成: 一、GC相关暂停 1. 安全点(Safepoint)阻塞 现象:JVM暂停但无GC日志,日志显示No GCs detected。原因:JVM等待所有线程进入安全点(如…...

【数据分析】R版IntelliGenes用于生物标志物发现的可解释机器学习
禁止商业或二改转载,仅供自学使用,侵权必究,如需截取部分内容请后台联系作者! 文章目录 介绍流程步骤1. 输入数据2. 特征选择3. 模型训练4. I-Genes 评分计算5. 输出结果 IntelliGenesR 安装包1. 特征选择2. 模型训练和评估3. I-Genes 评分计…...
Java数值运算常见陷阱与规避方法
整数除法中的舍入问题 问题现象 当开发者预期进行浮点除法却误用整数除法时,会出现小数部分被截断的情况。典型错误模式如下: void process(int value) {double half = value / 2; // 整数除法导致截断// 使用half变量 }此时...

Unity UGUI Button事件流程
场景结构 测试代码 public class TestBtn : MonoBehaviour {void Start(){var btn GetComponent<Button>();btn.onClick.AddListener(OnClick);}private void OnClick(){Debug.Log("666");}}当添加事件时 // 实例化一个ButtonClickedEvent的事件 [Formerl…...

Python训练营-Day26-函数专题1:函数定义与参数
题目1:计算圆的面积 任务: 编写一个名为 calculate_circle_area 的函数,该函数接收圆的半径 radius 作为参数,并返回圆的面积。圆的面积 π * radius (可以使用 math.pi 作为 π 的值)要求:函数接收一个位置参数 radi…...
高防服务器价格高原因分析
高防服务器的价格较高,主要是由于其特殊的防御机制、硬件配置、运营维护等多方面的综合成本。以下从技术、资源和服务三个维度详细解析高防服务器昂贵的原因: 一、硬件与技术投入 大带宽需求 DDoS攻击通过占用大量带宽资源瘫痪目标服务器,因此…...

Unity VR/MR开发-VR开发与传统3D开发的差异
视频讲解链接:【XR马斯维】VR/MR开发与传统3D开发的差异【UnityVR/MR开发教程--入门】_哔哩哔哩_bilibili...