Skip to content

Instantly share code, notes, and snippets.

@shkfnly
Created January 16, 2015 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shkfnly/db661fec5033e0da59ee to your computer and use it in GitHub Desktop.
Save shkfnly/db661fec5033e0da59ee to your computer and use it in GitHub Desktop.
DFS in Ruby
def dfs(node, val)
return node if node.value == val
node.children.each do |child|
result = dfs(child, val)
return result unless result.nil?
end
nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment