Skip to content

Instantly share code, notes, and snippets.

@brunofarache
Last active November 13, 2020 14:57
Show Gist options
  • Save brunofarache/6c1abfa81fb475c85ab5727b906ee856 to your computer and use it in GitHub Desktop.
Save brunofarache/6c1abfa81fb475c85ab5727b906ee856 to your computer and use it in GitHub Desktop.
def connect(root: Node): Node = {
def connectEdges(left: Node, right: Node): Unit = {
if (left == null) return
println(left.value)
left.next = right
connectEdges(left.right, right.left)
}
def visit(node: Node): Unit = {
if (node == null || node.left == null) return
println(node.value)
node.left.next = node.right
connectEdges(node.left.right, node.right.left)
visit(node.left)
visit(node.right)
}
visit(root)
root
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment