g2301_2400.s2331_evaluate_boolean_binary_tree.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g2301_2400.s2331_evaluate_boolean_binary_tree
// #Easy #Depth_First_Search #Tree #Binary_Search
// #2023_07_01_Time_199_ms_(91.67%)_Space_37.7_MB_(83.33%)
import com_github_leetcode.TreeNode
class Solution {
fun evaluateTree(root: TreeNode?): Boolean {
return if (root!!.left == null) {
root.`val` == 1
} else {
if (root.`val` == 2) {
evaluateTree(root.left) || evaluateTree(root.right)
} else {
evaluateTree(root.left) && evaluateTree(root.right)
}
}
}
}