Offer 59: Print binary trees in zigzag order: [[1], [3,2], [4,5,6,7]]

Keywords: C++

1 Topic Description

Please realize a function to print the two fork tree according to the zig zag pattern, that is, the first row is printed in the order from left to right, the second layer is printed in the right to left order, and the third line is printed in the order from left to right.

2 Thoughts and Methods

Given a binary tree style:

The output styles are: [[1], [3,2], [4,5,6,7]. It contains the following information: (1) the tree nodes contained in each layer; (2) the tree nodes in even layer need to be inverted.

Thoughts: There are also two ways to solve the problem of even-level inversion. The first way is to reverse the even-level node value (first save right, then save left to realize inversion) after obtaining the values of all nodes. The second is to store the values of the nodes in reverse order when they are acquired. Define two stack stack1 and stack2, while traversing the current layer node, stack1.empty! (TreeNode) *data=stack1.top (); store the next layer of nodes (stack2.push (data->right), stack2.push (data->left)), and so on, stack2.empty stack2.empty TreeNode TreeNode (); store the next layer of nodes. _

3 C++ Core Code

 1 /*
 2 struct TreeNode {
 3     int val;
 4     struct TreeNode *left;
 5     struct TreeNode *right;
 6     TreeNode(int x) :
 7             val(x), left(NULL), right(NULL) {
 8     }
 9 };
10 */
11 class Solution {
12 public:
13     vector<vector<int> > Print(TreeNode* pRoot) {
14         vector<vector<int>> result;
15         if(pRoot==nullptr)
16             return result;
17         stack<TreeNode*> stack1,stack2;//Store odd and even layers, respectively
18         stack1.push(pRoot);
19         while(!stack1.empty() || !stack2.empty()){
20             if(!stack1.empty()){
21                 vector<int> temp;
22                 while(!stack1.empty()){
23                     TreeNode *data=stack1.top();
24                     stack1.pop();
25                     temp.push_back(data->val);
26                     if(data->left!=nullptr)
27                         stack2.push(data->left);
28                     if(data->right!=nullptr)
29                         stack2.push(data->right);
30                 }
31                 result.push_back(temp);
32             }
33             if(!stack2.empty()){
34                 vector<int> temp;
35                 while(!stack2.empty()){
36                     TreeNode *data=stack2.top();
37                     stack2.pop();
38                     temp.push_back(data->val);
39                     if(data->right!=nullptr)
40                         stack1.push(data->right);
41                     if(data->left!=nullptr)
42                         stack1.push(data->left);
43                 }
44                 result.push_back(temp);
45             }
46         }
47         return result;
48     }
49 };

Reference material

https://blog.csdn.net/u010005281/article/details/79759926

 

Posted by mikesta707 on Sun, 06 Oct 2019 15:06:14 -0700