樹的非遞歸遍歷(層序)

層序是采用隊列的方式來遍歷的

就比如說上面這顆樹

他層序的就是: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);}

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/13655.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/13655.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/13655.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

簡析網絡風險量化的價值與應用實踐,如何構建網絡風險預防架構

網絡風險量化能夠讓公司董事會和高管層看清當前的網絡安全風險格局&#xff1b;它還將使安全團隊能夠在業務需求的背景下做出網絡安全決策&#xff0c;幫助組織確定哪些風險對業務構成最大的威脅&#xff0c;以及預期的經濟損失將是什么。 隨著網絡攻擊手段的日益多樣化和復雜…

多模態大模型新進展——GPT-4o、Project Astra關鍵技術丨青源Workshop第27期

青源Workshop丨No.27 多模態大模型新進展—GPT-4o、Project Astra關鍵技術主題閉門研討會 剛剛過去的兩天&#xff0c;OpenAI、Google紛紛發布了多模態大模型的最新成果&#xff0c;GPT-4o、Project Astra先后亮相。 本周五&#xff08;北京時間5月17日&#xff09;18點&#x…

O2OA(翱途)開發平臺數據統計如何配置?

O2OA提供的數據管理中心&#xff0c;可以讓用戶通過配置的形式完成對數據的匯總&#xff0c;統計和數據分組展現&#xff0c;查詢和搜索數據形成列表數據展現。也支持用戶配置獨立的數據表來適應特殊的業務的數據存儲需求。本文主要介紹如何在O2OA中開發和配置統計。 一、先決…

eNSP學習——OSPF多區域配置

目錄 主要命令 前期準備 實驗內容 分析 實驗目的 實驗步驟 實驗拓撲 實驗編址 實驗步驟 1、基本配置 配置與測試結果(部分) 2、配置骨干區域路由器 配置與測試結果(示例) 3、配置非骨干區域路由器 查看OSPF鄰居狀態 查看路由表中的OSPF路由條目 查看OSPF鏈…

【30天精通Prometheus:一站式監控實戰指南】第6天:mysqld_exporter從入門到實戰:安裝、配置詳解與生產環境搭建指南,超詳細

親愛的讀者們&#x1f44b; ??歡迎加入【30天精通Prometheus】專欄&#xff01;&#x1f4da; 在這里&#xff0c;我們將探索Prometheus的強大功能&#xff0c;并將其應用于實際監控中。這個專欄都將為你提供寶貴的實戰經驗。&#x1f680; ??Prometheus是云原生和DevOps的…

python設計模式--觀察者模式

觀察者模式是一種行為設計模式&#xff0c;它定義了一種一對多的依賴關系&#xff0c;讓多個觀察者對象同時監聽某一個主題對象&#xff0c;當主題對象狀態發生變化時&#xff0c;會通知所有觀察者對象&#xff0c;使它們能夠自動更新。 在 Python 中&#xff0c;觀察者模式通…

PersonalLLM——探索LLM是否能根據五大人格特質重新塑造一個新的角色?

1.概述 近年來&#xff0c;大型語言模型&#xff08;LLMs&#xff09;&#xff0c;例如ChatGPT&#xff0c;致力于構建能夠輔助人類的個性化人工智能代理&#xff0c;這些代理以進行類似人類的對話為重點。在學術領域&#xff0c;尤其是社會科學中&#xff0c;一些研究報告已經…

正心歸一、綻放真我 好普集團正一生命文化藝術大賽(中老年賽區)正式啟動

為進一步弘揚社會主義核心價值觀&#xff0c;弘揚生命文化&#xff0c;提升公眾對生命價值的認識與尊重&#xff0c;同時展現中老年藝術家的創作才華&#xff0c;激發廣大中老年朋友的藝術熱情和創造力。好普集團主辦&#xff0c;幸福金齡會與正一生命科學研究&#xff08;廣州…

adb獲取點擊坐標并模擬點擊事件(模擬滑動)

屏幕分辨率&#xff1a; $ adb shell wm size Physical size: 1080x2340 獲取設備的最大X和Y&#xff1a; 為8639 18719 $ adb shell getevent -p | grep -e "0035" -e "0036" 0035 : value 0, min 0, max 8639, fuzz 0, flat 0, resolution 0 0036 : v…

AWS安全性身份和合規性之Artifact

AWS Artifact是對您很重要的與合規性相關的信息的首選中央資源。AWS Artifact是一項服務&#xff0c;提供了一系列用于安全合規的文檔、報告和資源&#xff0c;以幫助用戶滿足其合規性和監管要求。它允許按需訪問來自AWS和在AWS Marketplace上銷售產品的ISV的安全性和合規性報告…

網絡模型-VLAN聚合

VLAN聚合 VLAN聚合(VLAN Aggregation,也稱SuperVLAN)指在一個物理網絡內&#xff0c;用多個VLAN(稱為Sub-VLAN)隔離廣播域并將這些Sub-VLAN聚合成一個邏輯的VLAN(稱為SuperVLAN)&#xff0c;這些Sub-VLAN使用同一個IP子網和缺省網關&#xff0c;&#xff0c;進而達到節約IP地址…

BOM..

區別&#xff1a;

基于BERT的醫學影像報告語料庫構建

大模型時代&#xff0c;任何行業&#xff0c;任何企業的數據治理未來將會以“語料庫”的自動化構建為基石。因此這一系列精選的論文還是圍繞在語料庫的建設以及自動化的構建。 通讀該系列的文章&#xff0c;猶如八仙過海&#xff0c;百花齊放。非結構的提取無外乎關注于非結構…

excel轉pdf并且加水印,利用ByteArrayOutputStream內存流不產生中間文件

首先先引入包&#xff1a;加水印和excel轉PDF的 <dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.12</version></dependency><dependency><groupId>org.apache.poi&l…

2024全新爆款好物推薦,618必買數碼好物清單吐血整理!

?距離618購物狂歡節越來越近了&#xff0c;有很多日常價格不菲的產品在這次活動期間都會進行促銷活動&#xff0c;尤其是數碼類產品&#xff0c;加上618的優惠活動更有吸引力了。不過面對大促的熱潮我們消費者在選購商品的同時還是要擦亮眼睛&#xff0c;避免買到質量不好的商…

SSE 與 SASE哪個云原生安全框架更加適合

近年來&#xff0c;隨著云計算和網絡技術的不斷發展&#xff0c;出現了一種新的網絡安全解決方案——SASE&#xff08;安全訪問服務邊緣&#xff09;。SASE是一種將網絡和安全功能融合到單個基于云的服務中的框架&#xff0c;旨在提供更加安全、高效和便捷的網絡訪問體驗。SASE…

云原生周刊:Flux 2.3 發布 | 2024.5.20

開源項目推薦 kubeinvaders kubeinvaders 專為 Kubernetes 用戶設計。它提供了一種有趣而交互式的方式來探索和可視化您的 Kubernetes 集群。通過類似游戲的界面&#xff0c;用戶可以瀏覽他們的集群&#xff0c;發現資源&#xff0c;甚至模擬對 Pod 的攻擊。通過 kubeinvader…

我的前端封裝之路

最近有粉絲提問了我一個面試中遇到的問題&#xff0c;他說面試的時候&#xff0c;面試官問我&#xff1a;你在以前的項目中封裝過組件嗎&#xff1f;或者做過npm公共庫嗎&#xff1f;遇到過什么問題嗎&#xff1f;當時自己突然覺得好像沒什么可回答的啊&#xff0c;但面試結束想…

前端 CSS 經典:弧形邊框選項卡

1. 效果圖 2. 開始 準備一個元素&#xff0c;將元素左上角&#xff0c;右上角設為圓角。 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8" /><meta name"viewport" content"widthdevice-width, i…

thingML的學習——什么是thingML

今天開始建模的學習&#xff0c;thingML是建模的一種工具 &#xff0c;也可以理解為一種建模語言&#xff0c;有自己的語法和語義。 ThingML 支持的多種平臺和通信協議&#xff0c;如UART、I2C、MQTT、WebSocket、REST、ROS、Bluetooth、BLE和Zwave&#xff0c;通過插件機制&a…