Verify Preorder Serialization of a Binary Tree

Posted HUSTLX

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Verify Preorder Serialization of a Binary Tree相关的知识,希望对你有一定的参考价值。

bool isValidSerialization(string preorder) {
    int len = preorder.size();
    vector<char> temp;
    bool flag = true;
    for (int i = 0; i < len; i++) {
        if (flag == true) {
            temp.push_back(preorder[i]);
            flag = false;
        }  
        if (preorder[i] == ‘,‘) {
            flag = true;
            continue;
        }
        int sz = temp.size();
        while (sz > 1 && temp[sz - 1] == ‘#‘&&temp[sz - 2] == ‘#‘) {
            temp.pop_back();
            temp.pop_back();
            if (temp.empty()) return false;
            temp.pop_back();       
            temp.push_back(‘#‘);
            sz = temp.size();
        }
    }
    return temp.size()==1&&temp[0]==‘#‘;
}

以上是关于Verify Preorder Serialization of a Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode Verify Preorder Serialization of a Binary Tree

255. Verify Preorder Sequence in Binary Search Tree

255. Verify Preorder Sequence in Binary Search Tree

Leetcode: Verify Preorder Serialization of a Binary Tree

LeetCode:Verify Preorder Serialization of a Binary Tree

331. Verify Preorder Serialization of a Binary Tree