[力扣] 算法 331 (C#)

331. 验证二叉树的前序序列化

public class Solution {
    public bool IsValidSerialization(string preorder) {
        var arr = preorder.Split(',');
        if (arr.Length == 0)
            return true;
        if (arr.Length % 2 == 0)
            return false;
        var degree = 1;
        foreach (var ch in arr) {
            if (degree == 0)
                return false;
            degree += (ch == "#" ? -1 : +1);
        }
        return degree == 0;
    }
}

发表评论