Wins the offer of a zigzag print order binary tree

Title Description

Implement according to a zigzag print function binary tree, i.e., left to right, the first print line of the second layer in order to print from right to left, the third line from left to right order of printing, in other rows forth.

Thinking

  1. Thought stack, the two stacks arranged, pressed into odd-numbered lines S1, S2 pressed into the even-numbered rows, calculates the current row in the odd rows using the COUNT is even, if the odd-numbered lines, the pop top of the stack S1, S2 be pressed into the top of the stack right, left and then pressed into the top of the stack, until the empty S1; if the even-numbered rows on the contrary, S2 pop the stack, and then pressed to the left and right pressure. After completing the count ++, remember the need to determine whether res_p is empty, if it is empty, it is already in the end
  2. Thought queue, according to the idea of ​​traversal sequence, if the output layer is an even number, the odd layers of the positive output reverse

Code

method one:

class Solution {
public:
    vector<vector<int> > Print(TreeNode* pRoot) {
        vector<vector<int> > res;
        vector<int> res_p;
        if(pRoot == NULL)
            return res;
        stack<TreeNode*> S1_tree;
        stack<TreeNode*> S2_tree;
        
        S1_tree.push(pRoot);
        res_p.push_back(pRoot->val);
        res.push_back(res_p);
        res_p.clear();
        
        int count = 0;
        while(S1_tree.size()>0 || S2_tree.size()>0)
        {
            if(count%2 == 0)
            {
                while(S1_tree.size() > 0)
                {
                    TreeNode* current = S1_tree.top();
                    S1_tree.pop();
                    if(current->right)
                    {
                        S2_tree.push(current->right);
                        res_p.push_back(current->right->val);
                    }
                    if(current->left)
                    {
                        S2_tree.push(current->left);
                        res_p.push_back(current->left->val);
                    }
                }
                if(res_p.size()>0)
                {
                    res.push_back(res_p);
                    res_p.clear();
                }
                count++;
            }
            else
            {
                while(S2_tree.size() > 0)
                {
                    TreeNode* current = S2_tree.top();
                    S2_tree.pop();
                    if(current->left)
                    {
                        S1_tree.push(current->left);
                        res_p.push_back(current->left->val);
                    }
                    if(current->right)
                    {
                        S1_tree.push(current->right);
                        res_p.push_back(current->right->val);
                    }
                }
                if(res_p.size()>0)
                {
                    res.push_back(res_p);
                    res_p.clear();
                }
                count++;
            }
        }
        return res;
    }
};

Method Two:

class Solution {
public:
    vector<vector<int> > Print(TreeNode* pRoot) {
        vector<vector<int> > res;
        vector<int> res_p;
        if(pRoot == NULL)
            return res;
        
        queue<TreeNode*> q_Tree;
        q_Tree.push(pRoot);
        res_p.push_back(pRoot->val);
        res.push_back(res_p);
        res_p.clear();
        int count = 1;
        
        while(q_Tree.size()>0)
        {
            int number = q_Tree.size();
            while(number > 0)
            {
                TreeNode* current = q_Tree.front();
                q_Tree.pop();
                if(current->left)
                {
                    q_Tree.push(current->left);
                    res_p.push_back(current->left->val);
                }
                if(current->right)
                {
                    q_Tree.push(current->right);
                    res_p.push_back(current->right->val);
                }
                number--;
            }
            if(count%2 == 0 && res_p.size()>0)
            {
                res.push_back(res_p);
                res_p.clear();
            }
            else if(count%2 == 1 && res_p.size()>0)
            {
                reverse(res_p.begin(),res_p.end());
                res.push_back(res_p);
                res_p.clear();
            }
            count++;
        }
        return res;
    }
};
Published 85 original articles · won praise 0 · Views 398

Guess you like

Origin blog.csdn.net/weixin_38312163/article/details/104832954