0965. Univalued Binary Tree

965. Univalued Binary Tree #

题目 #

如果二叉树的每个节点都具有相同的值,那么该二叉树就是 单值 二叉树。

只有给定的树是单值二叉树时,才返回 true,否则返回 false

思路 #

代码 #

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 preOrderTraversal(TreeNode root, int uniVal) {
        if (root == null) return true;
        if (root.val != uniVal) return false;
        return preOrderTraversal(root.left, uniVal) & preOrderTraversal(root.right, uniVal);
    }
    public boolean isUnivalTree(TreeNode root) {
        retur
    }
}