The hard part is having to store a counter just like Python. Since there is no way to access a variable that is outside the dfs function, so we can have a workaround by creating a Count class to store the max.
@count = count
count = Count.new()
return 0 if root.nil?
left, right = dfs(root.left,count), dfs(root.right,count)
count.count = [count.count,left + right].max
1 + [left,right].max