
DFS(3)
│
├── DFS(2)
│ │
│ ├── DFS(1)
│ │ │
│ │ ├── DFS(0)
│ │ │ │
│ │ └── print("1 ")
│ │
│ └── print("2 ")
│
└── print("3 ")
import Foundation
func DFS(_ x: Int) {
if x > 0 { return }
else {
DFS(x-1)
print(x, terminator: " ")
}
}
// Usage
if let n = Int(readLine() ?? "") {
DFS(n)
}
import Foundation
func dfs(_ x: Int) {
if x == 0 {
return
} else {
dfs(x / 2)
print(x % 2, terminator: "")
}
}
if let n = Int(readLine() ?? "") {
dfs(n)
}