Leetcode Note: Go - Root Equals Sum of Children

Root Equals Sum of Children - LeetCode
https://leetcode.com/problems/root-equals-sum-of-children/

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

回答

recursive - Root Equals Sum of Children - LeetCode
https://leetcode.com/problems/root-equals-sum-of-children/solutions/3063740/recursive/

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

     if root==nil {
         return true
     }
     sum:=0
     if root.Left!= nil {
         sum+=root.Left.Val
     }
      if root.Right!=nil {
          sum+=root.Right.Val
      }
      if sum==root.Val {
          return true
      } else {
          return false
      }
      sum=0
    return checkTree(root.Left) && checkTree(root.Right)
}