965. Univalued Binary Tree Easy

@problem@discussion
#Tree#Depth-First Search#Breadth-First Search#Binary Tree



1/**
2 * [965] Univalued Binary Tree
3 *
4 * A binary tree is uni-valued if every node in the tree has the same value.
5 * Given the root of a binary tree, return true if the given tree is uni-valued, or false otherwise.
6 *  
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2018/12/28/unival_bst_1.png" style="width: 265px; height: 172px;" />
9 * Input: root = [1,1,1,1,1,null,1]
10 * Output: true
11 * 
12 * Example 2:
13 * <img alt="" src="https://assets.leetcode.com/uploads/2018/12/28/unival_bst_2.png" style="width: 198px; height: 169px;" />
14 * Input: root = [2,2,2,5,2]
15 * Output: false
16 * 
17 *  
18 * Constraints:
19 * 
20 * 	The number of nodes in the tree is in the range [1, 100].
21 * 	0 <= Node.val < 100
22 * 
23 */
24pub struct Solution {}
25use crate::util::tree::{TreeNode, to_tree};
26
27// problem: https://leetcode.com/problems/univalued-binary-tree/
28// discuss: https://leetcode.com/problems/univalued-binary-tree/discuss/?currentPage=1&orderBy=most_votes&query=
29
30// submission codes start here
31
32// Definition for a binary tree node.
33// #[derive(Debug, PartialEq, Eq)]
34// pub struct TreeNode {
35//   pub val: i32,
36//   pub left: Option<Rc<RefCell<TreeNode>>>,
37//   pub right: Option<Rc<RefCell<TreeNode>>>,
38// }
39// 
40// impl TreeNode {
41//   #[inline]
42//   pub fn new(val: i32) -> Self {
43//     TreeNode {
44//       val,
45//       left: None,
46//       right: None
47//     }
48//   }
49// }
50use std::rc::Rc;
51use std::cell::RefCell;
52impl Solution {
53    pub fn is_unival_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
54        false
55    }
56}
57
58// submission codes end
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_965() {
66    }
67}
68


Back
© 2025 bowen.ge All Rights Reserved.