Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int n, *sb;
- void initializeTree() {
- cin >> n;
- sb = new int[n];
- sb[0] = n;
- for(int i=1; i<=n; i++) {
- cin >> sb[i];
- }
- }
- void printRoots() {
- for(int i=1; i<=n; i++) {
- if(sb[i] == 0) {
- cout << i << " is root\n";
- }
- }
- }
- void printLeafs() {
- for(int i=1; i<=n; i++) {
- for(int j=1; j<=n; j++) {
- if(i == sb[j]) {
- i++;
- continue;
- }
- if(j == n) {
- cout << i << " is a leaf\n";
- }
- }
- }
- }
- int main() {
- initializeTree();
- printRoots();
- printLeafs();
- delete sb;
- return EXIT_SUCCESS;
- }
- /*
- 8
- 0
- 1
- 8
- 2
- 4
- 2
- 1
- 6
- */
Advertisement
Add Comment
Please, Sign In to add comment