Skip to content

Instantly share code, notes, and snippets.

@lvjian700
Created March 4, 2024 01:59
Show Gist options
  • Save lvjian700/396e0180ce4f45a0c446fc2492c84967 to your computer and use it in GitHub Desktop.
Save lvjian700/396e0180ce4f45a0c446fc2492c84967 to your computer and use it in GitHub Desktop.
A kotlin DSL for building Binary Tree
data class TreeNode(
var `val`: Int,
var left: TreeNode? = null,
var right: TreeNode? = null
)
fun binaryTree(value: Int, init: TreeNode.() -> Unit) = TreeNode(value).apply(init)
fun TreeNode.left(value: Int, init: TreeNode.() -> Unit = {}) {
left = TreeNode(value).apply(init)
}
fun TreeNode.right(value: Int, init: TreeNode.() -> Unit = {}) {
right = TreeNode(value).apply(init)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment