Created
March 4, 2024 01:59
-
-
Save lvjian700/396e0180ce4f45a0c446fc2492c84967 to your computer and use it in GitHub Desktop.
A kotlin DSL for building Binary Tree
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
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