NC14 按之字形顺序打印二叉树

按之字形顺序打印二叉树

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
    vector<vector<int> > Print(TreeNode* pRoot) {
        vector<vector<int>> ans;
        if(!pRoot)return ans;
        stack<TreeNode*> s1;
        s1.push(pRoot);
        int seq=1;
        do{
            stack<TreeNode*> s2(s1);
            while(!s1.empty())s1.pop();
            vector<int> t;
            while(!s2.empty()){
                TreeNode* ln=s2.top();
                s2.pop();
                t.push_back(ln->val);
                if(seq%2){
                    if(ln->left)s1.push(ln->left);
                    if(ln->right)s1.push(ln->right);
                }else{
                    if(ln->right)s1.push(ln->right);
                    if(ln->left)s1.push(ln->left);
                }
            }
            seq++;
            ans.push_back(t);
        }while(!s1.empty());
        return ans;
    }
    
};
上一篇:openssl升级


下一篇:基本命令ln 软链接