Created
January 16, 2015 02:11
-
-
Save shkfnly/db661fec5033e0da59ee to your computer and use it in GitHub Desktop.
DFS in Ruby
This file contains 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
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