129. Sum Root to Leaf Numbers

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Note: For the purpose of this problem, we define empty string as valid palindrome.

Example 1:

Input: "A man, a plan, a canal: Panama"
Output: true

Example 2:

Input: "race a car"
Output: false



  • s consists only of printable ASCII characters.

Rust Solution

struct Solution;
use rustgym_util::*;

trait Preorder {
    fn preorder(&self, path: i32, sum: &mut i32);

impl Preorder for TreeLink {
    fn preorder(&self, path: i32, sum: &mut i32) {
        if let Some(node) = self {
            let node = node.borrow();
            let val = node.val;
            let path = path * 10 + val;
            if node.left.is_none() && node.right.is_none() {
                *sum += path;
            node.left.preorder(path, sum);
            node.right.preorder(path, sum);

impl Solution {
    fn sum_numbers(root: TreeLink) -> i32 {
        let mut res = 0;
        root.preorder(0, &mut res);

fn test() {
    let root = tree!(1, tree!(2), tree!(3));
    let res = 25;
    assert_eq!(Solution::sum_numbers(root), res);
    let root = tree!(4, tree!(9, tree!(5), tree!(1)), tree!(0));
    let res = 1026;
    assert_eq!(Solution::sum_numbers(root), res);

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