1. 程式人生 > >二叉樹的層序遍歷(遞迴和非遞迴)

二叉樹的層序遍歷(遞迴和非遞迴)

二叉樹的定義

typedef struct node{

struct node *left, *right;

int val;

}Node, *BinTree;

非遞迴實現二叉樹的層序遍歷:

void PrintTree(BinTree root)

{

if(!root)

return;

queue<Node *> q;

q.push(root);

BinTree p = root;

while(!q.empty())

{

p = q.front();

cout<<p->val<<" ";

q.pop();

if(p->left)

q.push(p->left)

if(p->right)

q.push(p->right)

}

}

非遞迴實現二叉樹的層序遍歷且每層各佔一行:

void PrintTreeLevel(BinTree root)

{

if(root == NULL)

return;

vector<Node *> vec;

vec.push_back(root);

int cur = 0;

int last = 1;

while(cur < vec.size())

{

last = vec.size();

while(cur < last)//為了分層輸出

{

cout<<vec[cur]->val<<" ";

if(vec[cur]->left != NULL)

vec.push_back(vec[cur]->left);

if(vec[cur]0>right != NULL)

vec.push_back(vec[cur]->right);

cur++;

}

cout<<endl;

}

}

遞迴實現二叉樹的層序遍歷:

class Solution {

public:

vector<vector<int>> levelorder(BinTree root)

{

vector<vector<int>> result;

level(result, root, 1);

return result;

}

}

void level(vector<vector<int>> &result, BinTree root, int num)

{

if(root ==NULL)

return;

if(num > result.size())

result.push_back(vector<int>());

result[num - 1].push_back(root->val);

level(result, root->left, num+1);

level(result, root->right. num+1);

}

相關推薦

關鍵詞:////層次

二叉樹層序遍歷 實現 def levelOrder(self, root): if root is None: return [] res = [] queue = [root]

【LeetCode-面試算法經典-Java實現】【107-Binary Tree Level Order Traversal IIII

lin -m length ret itl pub util 實現類 markdown 【107-Binary Tree Level Order Traversal II(二叉樹層序遍歷II)】 【LeetCode-面試算法經典-Java實現】【全

劍指Offer——:把列印成多行

對於二叉樹的最好的解決辦法就是遞迴。遍歷方法無外乎先序遍歷,中序遍歷,後序遍歷方法以及層序遍歷方法。這裡給大家安利一個關於樹的面試題的連結,博主walkinginthewind比較全面且詳細的介紹了二叉樹相關的面試題:對於層序遍歷,最好的方法就是用佇列記錄遍歷節點的值,按層列

Binary Tree Level Order Traversal-儲存並返回結果集

題目描述 Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). For example: Given

LeetCode | Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Gi

LeetCode 199 Binary Tree Right Side View

Given a binary tree, imagine yourself standing on therightside of it, return the values of the node

LeetCode | Binary Tree Level Order Traversal IIII

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf

node res queue pop left roo 實現 nod treenode 層序遍歷:用一個隊列保存當前結點的左右孩子以實現層序遍歷,因為先訪問的結點,其左右孩子結點也要先訪問 1 void LevelOrder(TreeNode* root,vector

for fin light list 先序 int eno 遞歸 none 二叉樹的先序遍歷(非遞歸)特別簡單 直接上代碼,根節點先入棧,然後循環棧不為空,pop出來後讓右節點和左節點分別入棧 # Definition for a binary tree node. #

資料結構實驗-C語言-二叉樹的建立,前、中、後序遍歷遞迴演算法和非遞迴演算法,求葉子結點數目,求二叉樹深度,判斷二叉樹是否相似,求二叉樹左右子互換,二叉樹層序遍歷的演算法,判斷二叉樹是否是完全二叉樹

1.實驗目的 熟練掌握二叉樹的二叉連結串列儲存結構的C語言實現。掌握二叉樹的基本操作-前序、中序、後序遍歷二叉樹的三種方法。瞭解非遞迴遍歷過程中“棧”的作用和狀態,而且能靈活運用遍歷演算法實現二叉樹的其它操作。 2.實驗內容 (1)二叉樹的二叉連結串列的建立 (2)二叉樹的前、中、後

與獲取深度的應用

不同於中序、前序、和後續遍歷使用棧,層序遍歷使用的是佇列! 程式碼如下: void level_order(tree_pointer ptr){ int front = rear = 0; tree_pointer queue[MAX_QUEUE_SIZE];

演算法及C語言實現

二叉樹後序遍歷的實現思想是:從根節點出發,依次遍歷各節點的左右子樹,直到當前節點左右子樹遍歷完成後,才訪問該節點元素。 圖 1 二叉樹   如圖 1 中,對此二叉樹進行後序遍歷的操作過程為: 從根節點 1 開始,遍歷該節點的左子樹(以節點 2 為根節點); 遍歷節點 2 的左子樹(以節點 4 為根

演算法及C語言實現

二叉樹中序遍歷的實現思想是: 訪問當前節點的左子樹; 訪問根節點; 訪問當前節點的右子樹; 圖 1 二叉樹   以圖  1 為例,採用中序遍歷的思想遍歷該二叉樹的過程為: 訪問該二叉樹的根節點,找到 1; 遍歷節點 1 的左子樹,找到節點 2; 遍歷節點 2 的左子樹,找到節點 4;

及C語言實現

二叉樹先序遍歷的實現思想是: 訪問根節點; 訪問當前節點的左子樹; 若當前節點無左子樹,則訪問當前節點的右子樹; 圖 1 二叉樹   以圖  1 為例,採用先序遍歷的思想遍歷該二叉樹的過程為: 訪問該二叉樹的根節點,找到 1; 訪問節點 1 的左子樹,找到節點 2; 訪問節點 2 的左子

LeetCode102

Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). For example: Given binary

[LeetCode] Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For ex

[LeetCode] Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9

leetcode 145+590 iterative附589,144,94

題目描述 145. Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,nu

應用之Binary Tree Right Side View

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to b

的c++寫法

void LevelOrder(struct node *root)//運用佇列(注意標頭檔案引用) {     queue<struct node*>q;     struct node *p = root;     if(p)     {         q