Created
March 31, 2020 11:29
-
-
Save tony9402/8fe69544fdaa4bde345459cdeb66da6b to your computer and use it in GitHub Desktop.
level-order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
struct Node{ | |
int root, left, right; | |
Node(int root=-1, int left=-1, int right=-1):root(root),left(left),right(right){ } | |
}; | |
Node tree[11]; | |
void makeEdge(int root, int left=-1, int right=-1){ | |
tree[root] = Node(root, left, right); | |
} | |
void level_order(int root){ | |
queue<int> q; | |
q.push(root); | |
while(!q.empty()){ | |
int qsize=q.size(); | |
while(qsize--){ | |
int now = q.front(); q.pop(); | |
cout << now << ' '; | |
if(tree[now].left != -1)q.push(tree[now].left); | |
if(tree[now].right != -1)q.push(tree[now].right); | |
} | |
} | |
} | |
int main(){ | |
//root of tree : 1 | |
//directed graph | |
makeEdge(1, 2, 3); | |
makeEdge(2, 4); | |
makeEdge(3, 5, 6); | |
makeEdge(5); | |
makeEdge(6, -1, 7); | |
makeEdge(4); | |
makeEdge(7); | |
//level-order | |
level_order(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment