算法练习(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;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/12993.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++设计模式::代理模式(combination)-可运行

实现: 1) cImage:抽象类; cImageReal:派生类, 不可直接实例化; cImageProxy:派生代理类, 可直接实例化用来代理cImageReal; NOTICE:派生代理类用来简化对特定派生类的使用. 使用: 实例化代理类, 然后使用. 1) 设计框架 /*image.hpp*/ #pragma once #…

java8 listmap聚合后内容顺序不变

学习了groupingBy的用法&#xff0c;在处理List<Map>的数据&#xff0c;顺序会发生变化&#xff1a; 直接聚合 public static void main(String[] args){String data "[{\"codeType\":\"ALRAM\",\"code\":\"1\",\"n…

MySQL笔记——表的分组查询、表的分页查询、表的约束、数据库设计

系列文章目录 MySQL笔记——MySQL数据库介绍以及在Linux里面安装MySQL数据库&#xff0c;对MySQL数据库的简单操作&#xff0c;MySQL的外接应用程序使用说明 MySQL笔记——表的修改查询相关的命令操作 MySQL案例——多表查询以及嵌套查询​​​​​​ MySQL笔记——数据库当…

【Kafka】消息队列Kafka进阶

目录 Kafka分区机制生产者分区写入策略轮询策略随机策略&#xff08;不用&#xff09;按key分配策略乱序问题自定义分区策略 消费者组Rebalance机制消费者分区分配策略Range范围分配策略RoundRobin轮询策略Stricky粘性分配策略 Kafka副本机制producer的ACKs参数acks配置为0acks…

three.js入门二:相机的zoom参数

环境&#xff1a; threejs&#xff1a;129 &#xff08;在浏览器的控制台下输入&#xff1a; window.__THREE__即可查看版本&#xff09;vscodewindowedge 透视相机或正交相机都有一个zoom参数&#xff0c;它可以用来将相机排到的内容在canvas上缩放显示。 要点&#xff1a;…

Mysql- 存储引擎

目录 1.Mysql体系结构 2.存储引擎简介 3.存储引擎特点 InnoDB MyISAM Memory 4.存储引擎选择 1.Mysql体系结构 MySQL整体的逻辑结构可以分为4层&#xff1a; 连接层&#xff1a;进行相关的连接处理、权限控制、安全处理等操作 服务层&#xff1a;服务层负责与客户层进行…

C++设计模式笔记

设计模式 如何解决复杂性&#xff1f; 分解 核心思想&#xff1a;分而治之&#xff0c;将大问题分解为多个小问题&#xff0c;将复杂问题分解为多个简单的问题。 抽象 核心思想&#xff1a;从高层次角度讲&#xff0c;人们处理复杂性有一个通用的技术&#xff0c;及抽象。…

博客迁移说明

后续博客的迁移说明 终于&#xff0c;我还是选择了迁移博客&#xff0c;本来只想改在简介的&#xff0c;但是被官方拒了&#xff0c;遂水此篇 C某N的限制太多&#xff0c;属实被恶心到了&#xff0c;不愿继续发博客在这上面学了使用Github Page搭建自己的博客&#xff0c;后续…

redis总结

1.redis redis高性能的key-value数据库&#xff0c;支持持久化&#xff0c;不仅仅支持简单的key-value&#xff0c;还提供了list&#xff0c;set&#xff0c;zset&#xff0c;hash等数据结构的存储&#xff0c;支持数据的备份&#xff08;master-slave模式&#xff09; redis&…

ShardingSphere-Proxy水平分片详解与实战

&#x1f680; ShardingSphere &#x1f680; &#x1f332; 算法刷题专栏 | 面试必备算法 | 面试高频算法 &#x1f340; &#x1f332; 越难的东西,越要努力坚持&#xff0c;因为它具有很高的价值&#xff0c;算法就是这样✨ &#x1f332; 作者简介&#xff1a;硕风和炜&…

【Sql】Sql Server 2008 数据库附加,错误:9004

【问题描述】 数据库文件存在异常状况&#xff0c;有可能是因为硬盘有坏区引起的。 附加数据库的时候&#xff0c;提示错误9004。 【解决方法】 假设数据库名称为&#xff1a;UFDATA_001_2023 请按顺序执行以下步骤&#xff1a; 1、将数据库的文件名&#xff1a;UFDATA_001_2…

Python爬虫时遇到SSL证书验证错误解决办法汇总

在进行Python爬虫任务时&#xff0c;遇到SSL证书验证错误是常见的问题之一。SSL证书验证是为了确保与服务器建立的连接是安全和可信的&#xff0c;但有时候可能会由于证书过期、不匹配或未受信任等原因导致验证失败。为了解决这个问题&#xff0c;本文将提供一些实用的解决办法…

【电路效应】信号处理和通信系统模型中的模拟电路效应研究(SimulinkMatlab代码实现)

目录 &#x1f4a5;1 概述 &#x1f4da;2 运行结果 &#x1f389;3 参考文献 &#x1f308;4 Matlab代码、Simulink仿真实现 &#x1f4a5;1 概述 在信号处理和通信系统模型中&#xff0c;模拟电路效应研究是指考虑到实际电路的特性对信号进行建模和分析的过程。模拟电路效应…

ffplay播放器剖析(6)----音视频同步分析

文章目录 1. 音视频同步基础1.1 音视频同步策略1.2 音视频同步概念1.3 FFmpeg中的时间单位1.4 不同结构体的time_base/duration分析1.5 不同结构体的pts/dts分析1.6 ffplay中Frame结构体分析1.7 Vidoe Frame PTS获取及矫正1.8 Audio Frame PTS的获取 2.以音频为基准3.以视频为基…

excel绘制折线图或者散点图

一、背景 假如现在通过代码处理了一批数据&#xff0c;想看数据的波动情况&#xff0c;是不是还需要写个pyhon代码&#xff0c;读取文件&#xff0c;绘制曲线&#xff0c;看起来也简单&#xff0c;但是还有更简单的方法&#xff0c;就是直接生成csv文件&#xff0c;csv文件就是…

Python 列表详解:从基础到进阶

Python是一种广泛使用的高级编程语言&#xff0c;它的设计强调代码的可读性和简洁的语法。Python支持多种编程范式&#xff0c;包括过程、面向对象和函数式编程。在Python中&#xff0c;列表是一种非常重要的数据类型&#xff0c;它可以包含各种类型的元素&#xff0c;如数字、…

Asp.Net 使用Log4Net (SQL Server)

Asp.Net 使用Log4Net (SQL Server) 1. 创建数据库表 首先&#xff0c;在你的SQL Server数据库中创建一个用于存储日志的表。以下是一个简单的表结构示例&#xff1a; CREATE TABLE [dbo].[Logs]([Id] [INT] IDENTITY(1,1) PRIMARY KEY,[Date] [DATETIME] NOT NULL,[Thread] …

解决安装office出现1402错误和注册表编辑器无法设置安全性错误

写在前面 可能是由于之前的office没有卸载干净&#xff0c;看了很多文章&#xff0c;也有的说是使用了Windows Installer Clean Up卸载office的缘故&#xff0c;最后导致的结果是出现了再次安装office时出现了1402错误&#xff0c;而在解决1402错误的过程中&#xff0c;修改所…

【MySQL】事务之MVCC(多版本并发控制)

【MySQL】事务-MVCC 一、数据库并发的三种场景二、MVCC2.1 3个记录隐藏字段2.2 undo log&#xff08;撤销日志&#xff09;2.3 模拟MVCC---update2.3.1 delete2.3.2 insert2.3.3 select 2.4 Read View2.5 整体流程 三、RR&#xff08;可重复读&#xff09;与RC&#xff08;读提…

【3-D深度学习:肺肿瘤分割】创建和训练 V-Net 神经网络,并从 3D 医学图像中对肺肿瘤进行语义分割研究(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…