DDSA
Advertisement

965. Univalued Binary Tree

965.cs
C#
public class TreeNode
{
    public int val;
    public TreeNode left;
    public TreeNode right;
    public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null)
    {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution
{
    public bool IsUnivalTree(TreeNode root)
    {
        if (root == null)
            return true;
        if (root.left != null && root.left.val != root.val)
            return false;
        if (root.right != null && root.right.val != root.val)
            return false;
        return IsUnivalTree(root.left) && IsUnivalTree(root.right);
    }
}
Advertisement
Was this solution helpful?