965. Univalued Binary Tree

A binary tree is univalued if every node in the tree has the same value.

Return true if and only if the given tree is univalued.

 

Example 1:

Input: [1,1,1,1,1,null,1]
Output: true

Example 2:

Input: [2,2,2,5,2]
Output: false

 

Note:

  1. The number of nodes in the given tree will be in the range [1, 100].
  2. Each node's value will be an integer in the range [0, 99].

Rust Solution

struct Solution;
use rustgym_util::*;

trait Preorder {
    fn is_unival(&self) -> bool;
    fn preorder(&self, val: &mut Option<i32>) -> bool;
}

impl Preorder for TreeLink {
    fn is_unival(&self) -> bool {
        let mut val: Option<i32> = None;
        self.preorder(&mut val)
    }
    fn preorder(&self, val: &mut Option<i32>) -> bool {
        if let Some(node) = self {
            let node = node.borrow();
            let node_val = node.val;
            if let Some(val) = val {
                if *val != node_val {
                    return false;
                }
            } else {
                *val = Some(node_val);
            }
            node.left.preorder(val) && node.right.preorder(val)
        } else {
            true
        }
    }
}

impl Solution {
    fn is_unival_tree(root: TreeLink) -> bool {
        root.is_unival()
    }
}

#[test]
fn test() {
    let root = tree!(1, tree!(1, tree!(1), tree!(1)), tree!(1, None, tree!(1)));
    assert_eq!(Solution::is_unival_tree(root), true);
    let root = tree!(2, tree!(2, tree!(5), tree!(2)), tree!(2));
    assert_eq!(Solution::is_unival_tree(root), false);
}

Having problems with this solution? Click here to submit an issue on github.