码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 103. Binary Tree Zigzag Level Order Traversal

时间:2016-05-17 22:37:37      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
 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

 

Leetcode 103. Binary Tree Zigzag Level Order Traversal

标签:

原文地址:http://www.cnblogs.com/qinduanyinghua/p/5503432.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!