LeetCode 1145 Binary Tree Coloring Game

Problem Statement


use std::rc::Rc;
use std::cell::RefCell;
use std::cmp::max;
impl Solution {
pub fn btree_game_winning_move(root: Option<Rc<RefCell<TreeNode>>>, n: i32, x: i32) -> bool {
fn count_sub_tree_nodes(node: Option<&Rc<RefCell<TreeNode>>>, x : i32,
left : &mut i32, right : &mut i32) -> i32 {
if let Some(n) = node {
let l = count_sub_tree_nodes(n.borrow().left.as_ref(), x, left, right);
let r = count_sub_tree_nodes(n.borrow().right.as_ref(), x, left, right);
if x == n.borrow().val {
*left = l; *right = r;
}
return l + r + 1;
} else {
return 0;
}
};

let mut left = 0;
let mut right = 0;
let count = count_sub_tree_nodes(root.as_ref(), x, &mut left, &mut right);
return max(max(left, right), n - left - right - 1) > n / 2;
}
}