Created
April 25, 2020 11:05
-
-
Save misterpoloy/04e5196523e309f7e50ef8b1d3152b34 to your computer and use it in GitHub Desktop.
Implement Depth Search in a Node
This file contains hidden or 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
// https://www.algoexpert.io/questions/Depth-first%20Search | |
#include <vector> | |
using namespace std; | |
// Do not edit the class below except | |
// for the depthFirstSearch method. | |
// Feel free to add new properties | |
// and methods to the class. | |
class Node { | |
public: | |
string name; | |
vector<Node *> children; | |
Node(string str) { name = str; } | |
// ACTUAL IMPLEMENTATION | |
// Time = BigO(vertices + edges) Space = BigO(edges) | |
vector<string> depthFirstSearch(vector<string> *array) { | |
array->push_back(this->name); // This is a pointer. | |
for (Node* node : this->children) { | |
node->depthFirstSearch(array); // Implement function in it's node method of children. | |
} | |
return *array; | |
} | |
Node *addChild(string name) { | |
Node *child = new Node(name); | |
children.push_back(child); | |
return this; | |
} | |
}; |
Author
misterpoloy
commented
Apr 25, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment