0965 - Univalued Binary Tree
Description (leetcode)
A binary tree is uni-valued if every node in the tree has the same value.
Given the root of a binary tree, return true if the given tree is uni-valued, or false otherwise.
Example 1:

Input: root = [1,1,1,1,1,null,1]
Output: true
Example 2:

Input: root = [2,2,2,5,2]
Output: false
Constraints:
- The number of nodes in the tree is in the range
[1, 100]. 0 <= Node.val < 100
submission
// 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 {
pub fn is_unival_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, val: i32) -> bool {
root.as_ref().is_none_or(|node| {
let node = node.borrow();
node.val == val && dfs(&node.left, val) && dfs(&node.right, val)
})
}
root.as_ref().is_none_or(|node| {
let node = node.borrow();
dfs(&node.left, node.val) && dfs(&node.right, node.val)
})
}
}