Leetcode 103. Binary Tree Zigzag Level Order Traversal

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
13         vector<int> row;
14         vector<vector<int>> v;
15         queue<TreeNode *> q;
16         if(root == NULL)
17             return v;
18         q.push(root);
19         TreeNode *temp;
20         int lev = 0;
21         while(!q.empty()) {
22             int size = q.size();
23             while(size--) {
24                 temp = q.front();
25                 q.pop();
26                 row.push_back(temp->val);
27                 if(temp->left != NULL) {
28                     q.push(temp->left);
29                 }
30                 if(temp->right != NULL) {
31                     q.push(temp->right);
32                 }
33             }
34             if(lev % 2) {
35                 int n = row.size();
36                 for(int i = 0; i < n/2; i++) {
37                     swap(row[i], row[n-i-1]);
38                 }
39             }
40             v.push_back(row);
41             lev++;
42             row.clear();
43         }
44         return v;
45     }
46 };
View Code

 

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).

For example: Given binary tree {3,9,20,#,#,15,7},

    3
   / \
  9  20
    /  \
   15   7

 

return its zigzag level order traversal as:

[
  [3],
  [20,9],
  [15,7]
]


分析:和层序遍历一样的代码,只需要加几行代码就行~~~因为要之字型存储这个二叉树~~~
所以只不过在行数为双数的时候需要对当前行进行一次所有元素的倒置~可以用stack也可以用数组头尾两两交换的方法~~
只需要在存入二维数组vector<vector> v之前倒置好row数组,再push_back到v里面就行~
 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
13         vector<int> row;
14         vector<vector<int>> v;
15         queue<TreeNode *> q;
16         if(root == NULL)
17             return v;
18         q.push(root);
19         TreeNode *temp;
20         int lev = 0;
21         while(!q.empty()) {
22             int size = q.size();
23             while(size--) {
24                 temp = q.front();
25                 q.pop();
26                 row.push_back(temp->val);
27                 if(temp->left != NULL) {
28                     q.push(temp->left);
29                 }
30                 if(temp->right != NULL) {
31                     q.push(temp->right);
32                 }
33             }
34             if(lev % 2) {
35                 int n = row.size();
36                 for(int i = 0; i < n/2; i++) {
37                     swap(row[i], row[n-i-1]);
38                 }
39             }
40             v.push_back(row);
41             lev++;
42             row.clear();
43         }
44         return v;
45     }
46 };
View Code

 


posted @ 2016-05-17 22:21  琴影  阅读(129)  评论(0编辑  收藏  举报