Created
March 31, 2020 11:24
-
-
Save tony9402/45210d2dc69b6bd0cb7c3f24608d270d to your computer and use it in GitHub Desktop.
inorder
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 inorder(int root){ | |
if(tree[root].left != -1)inorder(tree[root].left); | |
cout << root << ' '; | |
if(tree[root].right != -1)inorder(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); | |
//inorder : left -> root -> right | |
inorder(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment