# [965. 单值二叉树](https://leetcode.cn/problems/univalued-binary-tree) [English Version](/solution/0900-0999/0965.Univalued%20Binary%20Tree/README_EN.md) ## 题目描述 <!-- 这里写题目描述 --> <p>如果二叉树每个节点都具有相同的值,那么该二叉树就是<em>单值</em>二叉树。</p> <p>只有给定的树是单值二叉树时,才返回 <code>true</code>;否则返回 <code>false</code>。</p> <p> </p> <p><strong>示例 1:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0965.Univalued%20Binary%20Tree/images/screen-shot-2018-12-25-at-50104-pm.png" style="height: 159px; width: 200px;"></p> <pre><strong>输入:</strong>[1,1,1,1,1,null,1] <strong>输出:</strong>true </pre> <p><strong>示例 2:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0965.Univalued%20Binary%20Tree/images/screen-shot-2018-12-25-at-50050-pm.png" style="height: 158px; width: 200px;"></p> <pre><strong>输入:</strong>[2,2,2,5,2] <strong>输出:</strong>false </pre> <p> </p> <p><strong>提示:</strong></p> <ol> <li>给定树的节点数范围是 <code>[1, 100]</code>。</li> <li>每个节点的值都是整数,范围为 <code>[0, 99]</code> 。</li> </ol> ## 解法 <!-- 这里可写通用的实现逻辑 --> <!-- tabs:start --> ### **Python3** <!-- 这里可写当前语言的特殊实现逻辑 --> ```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def isUnivalTree(self, root: TreeNode) -> bool: def dfs(node): if node is None: return True return node.val == root.val and dfs(node.left) and dfs(node.right) return dfs(root) ``` ### **Java** <!-- 这里可写当前语言的特殊实现逻辑 --> ```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, root.val); } private boolean dfs(TreeNode root, int val) { if (root == null) { return true; } return root.val == val && dfs(root.left, val) && dfs(root.right, val); } } ``` ### **C++** ```cpp /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: bool isUnivalTree(TreeNode* root) { return dfs(root, root->val); } bool dfs(TreeNode* root, int val) { if (!root) return true; return root->val == val && dfs(root->left, val) && dfs(root->right, val); } }; ``` ### **Go** ```go /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func isUnivalTree(root *TreeNode) bool { var dfs func(*TreeNode) bool dfs = func(node *TreeNode) bool { if node == nil { return true } return node.Val == root.Val && dfs(node.Left) && dfs(node.Right) } return dfs(root) } ``` ### **TypeScript** ```ts /** * Definition for a binary tree node. * class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } * } */ function isUnivalTree(root: TreeNode | null): boolean { const val = root.val; const dfs = (root: TreeNode | null) => { if (root == null) { return true; } return root.val === val && dfs(root.left) && dfs(root.right); }; return dfs(root.left) && dfs(root.right); } ``` ### **Rust** ```rust // Definition for a binary tree node. // #[derive(Debug, PartialEq, Eq)] // pub struct TreeNode { // pub val: i32, // pub left: Option<Rc<RefCell<TreeNode>>>, // pub right: Option<Rc<RefCell<TreeNode>>>, // } // // impl TreeNode { // #[inline] // pub fn new(val: i32) -> Self { // TreeNode { // val, // left: None, // right: None // } // } // } use std::rc::Rc; use std::cell::RefCell; impl Solution { fn dfs(val: i32, root: &Option<Rc<RefCell<TreeNode>>>) -> bool { if root.is_none() { return true; } let root = root.as_ref().unwrap().borrow(); root.val == val && Self::dfs(val, &root.left) && Self::dfs(val, &root.right) } pub fn is_unival_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool { let root = root.as_ref().unwrap().borrow(); Self::dfs(root.val, &root.left) && Self::dfs(root.val, &root.right) } } ``` ### **...** ``` ``` <!-- tabs:end -->