树的非递归遍历(层序)
层序是采用队列的方式来遍历的
就比如说上面这颗树
他层序的就是:1 24 356
void LevelOrder(BTNode* root)
{Que q;QueueInit(&q);if (root){QueuePush(&q, root);}while (!QueueEmpty(&q)){BTNode* front = QueueFront(&q);QueuePop(&q);printf("%d ", front->val);if (front->left){QueuePush(&q, front->left);}if (front->right){QueuePush(&q, front->right);}}}
打印NULL ,这里front也会把NULL带进去
void LevelOrder(BTNode* root)
{Que q;QueueInit(&q);if (root){QueuePush(&q, root);}while (!QueueEmpty(&q)){BTNode* front = QueueFront(&q);QueuePop(&q);if (front){printf("%d ", front->val);QueuePush(&q, front->left);QueuePush(&q, front->right);}else{printf("N ");}}}
完整代码(栈和队列是复制前几篇的代码)
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int BinTreeType;
struct BinTreeNode
{struct BinTreeNode* left;struct BinTreeNode* right;BinTreeType val;};
typedef struct BinTreeNode BTNode;BTNode* BuyBTNode(BinTreeType val);
BTNode* CreateTree();
void PreOrder(BTNode* root);
void InOrder(BTNode* root);
void PostOrder(BTNode* root);
int TreeSize(BTNode* root);
int MaxDepth(BTNode* root);
int TreeLevel(BTNode* root, int k);
BTNode* TreeFind(BTNode* root, int x);
int BinaryTreeLeafSize(BTNode* root);
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
#include"BinTree.h"
typedef BTNode* QueueData;
struct QueueNode
{QueueData val;struct QueueNode* next;
};
typedef struct QueueNode QNode;
typedef struct Queue
{QNode* phead;QNode* ptail;int size;
}Que;
void QueueInit(Que* pq);//队列初始化
void QueueDestroy(Que* pq);//队列销毁void QueuePush(Que* pq,QueueData x);//入队列
void QueuePop(Que* pq);//出队列QueueData QueueBack(Que* pq);
QueueData QueueFront(Que* pq);
bool QueueEmpty(Que* pq);
int QueueSize(Que* pq);
#define _CRT_SECURE_NO_WARNINGS 1
#include"BinTree.h"
BTNode* BuyBTNode(BinTreeType val)
{BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));if (newnode == NULL){perror("malloc fail!");exit(1);}newnode->val = val;newnode->left = NULL;newnode->right = NULL;return newnode;
}
BTNode* CreateTree()
{BTNode* n1 = BuyBTNode(1);BTNode* n2 = BuyBTNode(2);BTNode* n3 = BuyBTNode(3);BTNode* n4 = BuyBTNode(4);BTNode* n5 = BuyBTNode(5);BTNode* n6 = BuyBTNode(6);n1->left = n2;n1->right = n4;n2->left = n3;n2->right = NULL;n3->left = NULL;n3->right = NULL;n4->left = n5;n4->right = n6;n5->left = NULL;n5->right = NULL;n6->left = NULL;n6->right = NULL;return n1;
}
void PreOrder(BTNode* root)
{if (root == NULL){printf("NULL ");return ;}printf("%d ", root->val);PreOrder(root->left);PreOrder(root->right);
}
void InOrder(BTNode* root)
{if (root == NULL){printf("NULL ");return;}InOrder(root->left);printf("%d ", root->val);InOrder(root->right);
}
void PostOrder(BTNode* root)
{if (root == NULL){printf("NULL ");return;}PostOrder(root->left);PostOrder(root->right);printf("%d ", root->val);
}
int TreeSize(BTNode* root)
{if (root == NULL){return 0;}else{return TreeSize(root->left) + TreeSize(root->right)+1;}}int MaxDepth(BTNode* root)
{if (root == NULL){return 0;}int leftDepth = MaxDepth(root->left);int rightDepth = MaxDepth(root->left);if (leftDepth > rightDepth){return leftDepth + 1;}else{return rightDepth + 1;}
}
int TreeLevel(BTNode* root, int k)
{assert(k > 0);if (root == NULL){return 0;}if (k == 1){return 1;}return TreeLevel(root->left, k - 1) + TreeLevel(root->right, k - 1);
}
BTNode* TreeFind(BTNode* root, int x)
{if (root == NULL){return NULL;}if (root->val == x){return root;}BTNode* ret1 = TreeFind(root->left, x);if (ret1){return ret1;}return TreeFind(root->right, x); }
int BinaryTreeLeafSize(BTNode* root)
{if (root == NULL){return 0;}if (BinaryTreeLeafSize(root->left) == NULL && BinaryTreeLeafSize(root->right) == NULL){return 1; } return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
}
#define _CRT_SECURE_NO_WARNINGS 1
#include"queue.h"
void QueueInit(Que* pq)
{assert(pq);pq->phead = NULL;pq->ptail = NULL;pq->size = 0;
}
void QueueDestroy(Que* pq)
{QNode* pcur = pq->phead;while (pcur){QNode* next = pcur->next;free(pcur);pcur = next;}pq->phead = pq->ptail = NULL;pq->size = 0;
}void QueuePush(Que* pq,QueueData x)
{assert(pq);QNode* newnode = (Que*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail");exit(1);}newnode->val = x;newnode->next = NULL;if (pq->ptail){pq->ptail->next = newnode;pq->ptail = newnode;}else{pq->phead = pq->ptail = newnode;}pq->size++;
}
void QueuePop(Que* pq)
{assert(pq->phead != NULL);if (pq->phead->next == NULL){free(pq->phead);pq->phead =pq->ptail= NULL;}else{QNode* next = pq->phead->next;free(pq->phead); pq->phead = next;}pq->size--;
}QueueData QueueFront(Que* pq)
{assert(pq!=NULL);assert(pq->phead != NULL);return pq->phead->val;
}
QueueData QueueBack(Que* pq)
{assert(pq!=NULL);assert(pq->ptail != NULL);return pq->ptail->val;
}
bool QueueEmpty(Que* pq)
{return pq->phead == NULL;
}
int QueueSize(Que* pq)
{assert(pq);return pq->size;}
#define _CRT_SECURE_NO_WARNINGS 1
#include"BinTree.h"
#include"queue.h"
void LevelOrder(BTNode* root)
{Que q;QueueInit(&q);if (root){QueuePush(&q, root);}while (!QueueEmpty(&q)){BTNode* front = QueueFront(&q);QueuePop(&q);if (front){printf("%d ", front->val);QueuePush(&q, front->left);QueuePush(&q, front->right);}else{printf("N ");}}}
int main()
{BTNode* root= CreateTree();//PreOrder(root);printf("\n");LevelOrder(root);}
相关文章:

树的非递归遍历(层序)
层序是采用队列的方式来遍历的 就比如说上面这颗树 他层序的就是:1 24 356 void LevelOrder(BTNode* root) {Que q;QueueInit(&q);if (root){QueuePush(&q, root);}while (!QueueEmpty(&q)){BTNode* front QueueFront(&q);QueuePop(&q);print…...
解决SpringBoot使用@Transactional进行RestTemplate远程调用导致查询数据记录为null的bug
开启事务过程中,如果远程调用查询当前已经开启但没有提交的事务,就会查不到数据。 示例代码 import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.transaction.annotation.Transactional; import o…...
pl/sql基础语法操作
oracle pl/sql语言(procedural language/sql)是结合了结构化查询与oracle自身过程控制为一体的强大语言。 语法执行块 语法结构: [ declare 可选 声明变量部分--declaration statements (1);]begin --执行部分--executable statements (2)…...
Vue 父组件向子组件传递数据
1、在子组件中,你需要声明你期望从父组件接收哪些props。这可以通过props选项完成,可以是一个数组或对象形式: export default {props: [message],props:{message:String }props: {message: String, // 类型检查count: {type: Nu…...

二十五、openlayers官网示例CustomOverviewMap解析——实现鹰眼地图、预览窗口、小窗窗口地图、旋转控件
官网demo地址: Custom Overview Map 这个示例展示了如何在地图上增加一个小窗窗口的地图并跟随着地图的旋转而旋转视角。 首先加载了一个地图。其中 DragRotateAndZoom是一个交互事件,它可以实现按住shift键鼠标拖拽旋转地图。 const map new Map({int…...
K8S Secret管理之SealedSecrets
1 关于K8S Secret 我们通常将应用程序使用的密码、API密钥保存在K8S Secret中,然后应用去引用。对于这些敏感信息,安全性是至关重要的,而传统的存储方式可能会导致密钥在存储、传输或使用过程中受到威胁,例如在git中明文存储密码…...
Gone框架介绍25 - Redis模块参考文档
文章目录 Redis 参考文档配置项import 和 bury使用分布是缓存 redis.Cache接口定义使用示例 使用分布式锁 redis.Locker接口定义使用示例 操作Key,使用 redis.Key接口定义 使用 Provider 注入 redis 接口使用示例 直接使用redis连接池接口定义使用示例 Redis 参考文…...
SpringBoot前置知识02-spring注解发展史
springboot前置知识01-spring注解发展史 spring1.x spring配置只能通过xml配置文件的方式注入bean,需要根据业务分配配置文件,通过import标签关联。 spring1.2版本出现Transactional注解 <?xml version"1.0" encoding"UTF-8"?> <be…...

C++ TCP发送Socket数据
DEVC需要加入ws2_32库 #include <iostream> #include <winsock2.h>#pragma comment(lib, "ws2_32.lib")void sendData(const char* ip, int port, const char* data) {WSADATA wsaData;SOCKET sockfd;struct sockaddr_in server_addr;// 初始化Winsock…...

鸿蒙HarmonyOS开发中的易混点归纳-持续补充中
相关文章目录 鸿蒙HarmonyOS开发术语全解:小白也能看懂! 文章目录 相关文章目录前言一、build()函数和Builder装饰器?二、自定义组件和系统组件(内置组件)三、组件和页面四、自定义弹窗和其他弹窗总结 前言 一、build…...

ue引擎游戏开发笔记(45)——添加游戏音效
1.需求分析: 截至目前,我们仍然在一个无声的世界游玩游戏,所以有必要为游戏增添一些声音,例如开火声,子弹撞击声等等。 2.操作实现: 1.这是一个较为简单的功能,类似特效的实现方法,…...

202472读书笔记|《首先你要快乐,其次都是其次》——快乐至上,允许一切发生
202472读书笔记|《首先你要快乐,其次都是其次》——快乐至上,允许一切发生 《首先你要快乐,其次都是其次》作者林小仙,挺轻松的小漫画,清新的文字。 生而为人,我很抱歉,大可不必。 生活已经很难…...

8.STL中Vector容器的常见操作(附习题)
目录 1.vector的介绍 2 vector的使用 2.1 vector的定义 2.2 vector iterator 的使用 2.3 vector 空间增长问题 2.3 vector 增删查改 2.4 vector 迭代器失效问题 2.5 vector 在OJ中的使用 1.vector的介绍 vector是表示可变大小数组的序列容器。 就像数组一样࿰…...

5.23小结
1.java项目创新 目前想添加一个自动回复的功能和设置验证方式有(允许任何人添加,禁止添加,设置回答问题添加,普通验证添加) 目前只完成画好前端界面,前端发送请求,还有表的修改 因为涉及表字…...

文心一言 VS 讯飞星火 VS chatgpt (265)-- 算法导论20.1 4题
四、假设不使用一棵叠加的度为 u \sqrt{u} u 的树,而是使用一棵叠加的度为 u 1 k u^{\frac{1}{k}} uk1的树,这里 k 是大于 1 的常数,则这样的一棵树的高度是多少?又每个操作将需要多长时间?如果要写代码…...
Flutter 中的 EditableText 小部件:全面指南
Flutter 中的 EditableText 小部件:全面指南 在Flutter中,EditableText是一个低级别的文本编辑组件,它提供了构建自定义文本编辑界面的能力。与TextField和TextFormField不同,EditableText提供了更多的灵活性,允许开发…...

H800基础能力测试
H800基础能力测试 参考链接A100、A800、H100、H800差异H100详细规格H100 TensorCore FP16 理论算力计算公式锁频安装依赖pytorch FP16算力测试cublas FP16算力测试运行cuda-samples 本文记录了H800基础测试步骤及测试结果 参考链接 NVIDIA H100 Tensor Core GPU Architecture…...
2024/5/24 Day38 greedy 435. 无重叠区间 763.划分字母区间 56. 合并区间
2024/5/24 Day38 greedy 435. 无重叠区间 763.划分字母区间 56. 合并区间 遇到两个维度权衡的时候,一定要先确定一个维度,再确定另一个维度。如果两个维度一起考虑一定会顾此失彼。 重叠区间问题 435. 无重叠区间 题目链接 435 给定一个区间的集合 i…...
【python】使用函数名而不加括号是什么情况?
使用函数名而不加括号通常是为了表示对函数本身的引用,而不是调用函数。这种用法通常出现在下面这几种情况: 作为回调函数传递:将函数名作为参数传递给其他函数,以便在需要时调用该函数。例如,在事件处理程序或高阶函数…...
全文检索ElasticSearch简介
1 全文检索 1.1 什么是全文检索 全文检索是一种通过对文本内容进行全面索引和搜索的技术。它可以快速地在大量文本数据中查找包含特定关键词或短语的文档,并返回相关的搜索结果。全文检索广泛应用于各种信息管理系统和应用中,如搜索引擎、文档管理系统、电子邮件客户端、新闻…...
uniapp 对接腾讯云IM群组成员管理(增删改查)
UniApp 实战:腾讯云IM群组成员管理(增删改查) 一、前言 在社交类App开发中,群组成员管理是核心功能之一。本文将基于UniApp框架,结合腾讯云IM SDK,详细讲解如何实现群组成员的增删改查全流程。 权限校验…...

(LeetCode 每日一题) 3442. 奇偶频次间的最大差值 I (哈希、字符串)
题目:3442. 奇偶频次间的最大差值 I 思路 :哈希,时间复杂度0(n)。 用哈希表来记录每个字符串中字符的分布情况,哈希表这里用数组即可实现。 C版本: class Solution { public:int maxDifference(string s) {int a[26]…...

调用支付宝接口响应40004 SYSTEM_ERROR问题排查
在对接支付宝API的时候,遇到了一些问题,记录一下排查过程。 Body:{"datadigital_fincloud_generalsaas_face_certify_initialize_response":{"msg":"Business Failed","code":"40004","sub_msg…...
利用ngx_stream_return_module构建简易 TCP/UDP 响应网关
一、模块概述 ngx_stream_return_module 提供了一个极简的指令: return <value>;在收到客户端连接后,立即将 <value> 写回并关闭连接。<value> 支持内嵌文本和内置变量(如 $time_iso8601、$remote_addr 等)&a…...
关于 WASM:1. WASM 基础原理
一、WASM 简介 1.1 WebAssembly 是什么? WebAssembly(WASM) 是一种能在现代浏览器中高效运行的二进制指令格式,它不是传统的编程语言,而是一种 低级字节码格式,可由高级语言(如 C、C、Rust&am…...

JUC笔记(上)-复习 涉及死锁 volatile synchronized CAS 原子操作
一、上下文切换 即使单核CPU也可以进行多线程执行代码,CPU会给每个线程分配CPU时间片来实现这个机制。时间片非常短,所以CPU会不断地切换线程执行,从而让我们感觉多个线程是同时执行的。时间片一般是十几毫秒(ms)。通过时间片分配算法执行。…...

SpringTask-03.入门案例
一.入门案例 启动类: package com.sky;import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCach…...

嵌入式学习笔记DAY33(网络编程——TCP)
一、网络架构 C/S (client/server 客户端/服务器):由客户端和服务器端两个部分组成。客户端通常是用户使用的应用程序,负责提供用户界面和交互逻辑 ,接收用户输入,向服务器发送请求,并展示服务…...

搭建DNS域名解析服务器(正向解析资源文件)
正向解析资源文件 1)准备工作 服务端及客户端都关闭安全软件 [rootlocalhost ~]# systemctl stop firewalld [rootlocalhost ~]# setenforce 0 2)服务端安装软件:bind 1.配置yum源 [rootlocalhost ~]# cat /etc/yum.repos.d/base.repo [Base…...

三分算法与DeepSeek辅助证明是单峰函数
前置 单峰函数有唯一的最大值,最大值左侧的数值严格单调递增,最大值右侧的数值严格单调递减。 单谷函数有唯一的最小值,最小值左侧的数值严格单调递减,最小值右侧的数值严格单调递增。 三分的本质 三分和二分一样都是通过不断缩…...