# 965. Univalued Binary Tree

T: O(n)

S: O(n)



```java
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isUnivalTree(TreeNode root) {
        return dfs(root.val, root);
    }
    private boolean dfs(int uniVal, TreeNode node) {
        if (node == null) {
            return true;
        }
        boolean result = node.val == uniVal;
        result &= dfs(uniVal, node.left);
        result &= dfs(uniVal, node.right);
        return result;
    }
}
```
