Created
March 31, 2020 11:23
-
-
Save tony9402/63817f4370e327b10a620cefa47e9b9e to your computer and use it in GitHub Desktop.
preorder
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 preorder(int root){ | |
cout << root << ' '; | |
if(tree[root].left != -1)preorder(tree[root].left); | |
if(tree[root].right != -1)preorder(tree[root].right); | |
} | |
int main(){ | |
//root of tree : 1 | |
makeEdge(1, 2, 3); | |
makeEdge(2, 4); | |
makeEdge(3, 5, 6); | |
makeEdge(5); | |
makeEdge(6, -1, 7); | |
makeEdge(4); | |
makeEdge(7); | |
//preorder : root -> left -> right | |
preorder(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment