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