Niuke.com's sword refers to Offer - print binary tree in zigzag order

Topic description

Please implement a function to print the binary tree in a zigzag pattern, that is, the first line is printed in left-to-right order, the second layer is printed in right-to-left order, the third line is printed in left-to-right order, and the other lines are printed in order from left to right. And so on.

method one

The method is similar to printing a binary tree from top to bottom. The traversal order is from top to bottom, and each line is traversed from left to right, but a parameter row needs to be added to mark the current number of lines. If it is an even number of lines, each time Put the value at the end of the vector; if it is an odd row, insert the value into the first position of the vector each time.

code

/*
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>> res;
        if( pRoot == NULL )
            return res;
        queue<pair<TreeNode*,int>> q;
        q.push( make_pair(pRoot,0) );
        while( !q.empty() )
        {
            TreeNode* node = q.front().first;
            int row = q.front().second;
            q.pop();
            if( node == NULL )
                continue;
            if( res.size() <= row )
            {
                vector<int> temp;
                res.push_back(temp);
            }
            if( row%2 == 0 )
                res[row].push_back( node->val );
            else
                res[row].insert(res[row].begin(), node->val);
            q.push( make_pair(node->left, row+1) );
            q.push( make_pair(node->right, row+1) );
        }
        return res;
    }
     
};

Method Two

According to the meaning of the question, the access order of the nodes in each row is reversed. We can use two stacks to store them in alternate rows. One stack accesses the top element of the other stack according to the order of "left node -> right node". And the other stack accesses the top element of the other stack according to the order of "right subtree -> left subtree" until both stacks are empty
Take the following binary tree as an example:
1                  8
2                /  \
3               6   10
4              / \  / \
5             5  7 9  11

1、首先,建立两个栈s1和s2,将根节点(8)存入s1中。返回值为vector<vector<int>> res;

2、然后,将s1中节点(即根节点8)弹出,将8存入res中,然后将其左子节点(6)和右子节点(10)存入s2中,此时s1为空,s2中元素为6、10;

3、将s2中的节点弹出,先弹出的节点为10,后弹出的节点为6。弹出10时,将10放入res,将其子节点按照先右子节点(11),后左子节点(9)的顺序压入s1;然后弹出节点6,同样,将6放入res,并将其右子节点(7)和左子节点(5)压入s1;此时s1中元素为11、9、7、5;

4、再对s1进行类似操作,可以看出最后一行输出顺序为5、7、9、11,符合题目要求。直到s1和s2均为空,说明树中所有节点已经遍历完成。

代码

/*
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>> res;
        if( pRoot == NULL )
            return res;
        stack<TreeNode*> s1;
        stack<TreeNode*> s2;
        int row = 0;
        s1.push( pRoot );
        while( !s1.empty() || !s2.empty() )
        {
            while( !s1.empty() )
            {
                TreeNode* node = s1.top();
                s1.pop();
                if( node == NULL )
                    continue;
                if( row >= res.size() )
                {
                    vector<int> temp;
                    res.push_back(temp);
                }
                res[row].push_back( node->val );
                s2.push(node->left);
                s2.push(node->right);
            }
            row++;
            while( !s2.empty() )
            {
                TreeNode* node = s2.top();
                s2.pop();
                if( node == NULL )
                    continue;
                if( row >= res.size() )
                {
                    vector<int> temp;
                    res.push_back(temp);
                }
                res[row].push_back( node->val );
                s1.push(node->right);
                s1.push(node->left);
            }
            row++;
        }
        
        return res;
    }
    
};

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325764907&siteId=291194637