Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #include <vector>
- #include <string>
- using namespace std;
- struct node {
- int value;
- vector<node*> children;
- node(int v) {
- value = v;;
- }
- };
- int dfs(node* n) {
- if (!n) {
- return 0;
- }
- cout << n->value << endl;
- int m = 0;
- for (node* it : n->children) {
- m = max(m, dfs(it));
- }
- return n->value + m;
- }
- int main()
- {
- node* n4 = new node(4);
- node* n12 = new node(12);
- node* n42 = new node(42);
- node* n9 = new node(9);
- node* n1 = new node(1);
- node* root = new node(5);
- n1->children.push_back(n4);
- n1->children.push_back(n12);
- n1->children.push_back(n42);
- root->children.push_back(n9);
- root->children.push_back(n1);
- int r = dfs(root);
- cout << r << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement