Leetcode Note: Go - Evaluate Boolean Binary Tree

Evaluate Boolean Binary Tree - LeetCode
https://leetcode.com/problems/evaluate-boolean-binary-tree/

  • Go 言語で取り組んだメモ

回答

Evaluate Boolean Binary Tree - LeetCode
https://leetcode.com/problems/evaluate-boolean-binary-tree/solutions/3252388/golang-clean-solution/

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func evaluateTree(root *TreeNode) bool {
    return dfs(root)
}

func dfs(root *TreeNode) bool{
    if root != nil{
        if root.Val < 2{
            return root.Val != 0
        } else{
            if root.Val == 2{
                return dfs(root.Left) || dfs(root.Right)
            }
            if root.Val == 3{
                return dfs(root.Left) && dfs(root.Right)
            }
        }
    }
    return true
}