Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created April 25, 2020 11:05
Show Gist options
  • Save misterpoloy/04e5196523e309f7e50ef8b1d3152b34 to your computer and use it in GitHub Desktop.
Save misterpoloy/04e5196523e309f7e50ef8b1d3152b34 to your computer and use it in GitHub Desktop.
Implement Depth Search in a Node
// 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;
}
};
@misterpoloy
Copy link
Author

explanation

breadth-first and depth-first strategies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment