BFS和DFS
2020-11-04 本文已影响0人
const_qiu
DFS
//C/C++
//递归写法:
map<int, int> visited;
void dfs(Node* root) {
// terminator
if (!root) return ;
if (visited.count(root->val)) {
// already visited
return ;
}
visited[root->val] = 1;
// process current node here.
// ...
for (int i = 0; i < root->children.size(); ++i) {
dfs(root->children[i]);
}
return ;
}
//C/C++
//非递归写法:
void dfs(Node* root) {
map<int, int> visited;
if(!root) return ;
stack<Node*> stackNode;
stackNode.push(root);
while (!stackNode.empty()) {
Node* node = stackNode.top();
stackNode.pop();
if (visited.count(node->val)) continue;
visited[node->val] = 1;
for (int i = node->children.size() - 1; i >= 0; --i) {
stackNode.push(node->children[i]);
}
}
return ;
}