Leetcode Note: Go - Univalued Binary Tree

Univalued Binary Tree - LeetCode
https://leetcode.com/problems/univalued-binary-tree/

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

回答

Univalued Binary Tree - LeetCode
https://leetcode.com/problems/univalued-binary-tree/solution/

0 ms, faster than 100.00% of Go online submissions for Univalued Binary Tree. - LeetCode Discuss
https://leetcode.com/problems/univalued-binary-tree/discuss/1213257/0-ms-faster-than-100.00-of-Go-online-submissions-for-Univalued-Binary-Tree.

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func isUnivalTree(root *TreeNode) bool {
	ans := root.Val
	helper965(root, &ans)
	if ans == -1 {
		return false
	}
	return true 
}

func helper965(node *TreeNode, flag *int)  {
	if node != nil {
		if node.Val != *flag {
			*flag = -1
		} else {
			helper965(node.Left, flag)
			helper965(node.Right, flag)
		}
	}
}