码迷,mamicode.com
首页 > 其他好文 > 详细

Verify Preorder Serialization of a Binary Tree

时间:2016-03-06 15:37:22      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/hustlx/p/5247435.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!