ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

剑指offer 按之字形顺序打印二叉树

2019-08-11 22:01:54  阅读:206  来源: 互联网

标签:之字形 TreeNode offer next current pNode 二叉树 push NULL


题目描述

请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。     思路: 1、有了之前层次遍历一行一行输出的经验,我们可以直接用一个变量记录行数,如果是奇数行,就将队列中的元素按顺序所有保存下来,如果是偶数行,就顺序保存到一个临时vector中,再逆序保存下来。
 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> > res;
15         vector<int> temp;
16         int odd = 0; //0表示奇数行,1表示偶数行
17         queue<TreeNode*> q;
18         if (pRoot == NULL)
19             return res;
20         q.push(pRoot);
21         int current = 1, next = 0; //current记录当前打印行的元素数,next表示下一行的元素个数。
22         while (!q.empty()) {
23             temp.clear();
24             for (int i = 0; i < current; i++) {
25                 TreeNode* pNode = q.front();
26                 q.pop();
27                 temp.push_back(pNode->val);
28                 if (pNode->left != NULL) {
29                     q.push(pNode->left);
30                     next++;
31                 }
32                 if (pNode->right != NULL) {
33                     q.push(pNode->right);
34                     next++;
35                 }
36             }
37             //如果是奇数行,就要将保存下来的逆序保存
38             if (odd == 1) {
39                 for (int i = 0; i < (current >> 1); i++) {
40                     swap(temp[i], temp[current - 1 - i]);
41                 }
42             }
43             res.push_back(temp);
44             odd = 1 - odd;
45             current = next;
46             next = 0;
47         }
48         return res;
49     }
50 };

 

    2、剑指offer思路
 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> > res;
15         vector<int> temp;
16         stack<TreeNode*> level[2]; 
17         int current = 0, next = 1; 
18         if (pRoot == NULL)
19             return res;
20         level[current].push(pRoot);
21         //如果pRoot == NULL, 那么压入栈之后,栈非空,而取元素的话,又会产生越界?
22         while (!level[current].empty() || !level[next].empty()) {
23             TreeNode* pNode = level[current].top();
24             level[current].pop();
25             temp.push_back(pNode->val);
26             //current == 0表示当前在保存偶数行的数,将下一行的数保存到栈时,先将节点的左孩子放入栈中
27             if (current == 0) {
28                 if (pNode->left != NULL)
29                     level[next].push(pNode->left);
30                 if (pNode->right != NULL)
31                     level[next].push(pNode->right);
32             } else { //当前在保存奇数行的数据,先将节点的右孩子放入栈中
33                 if (pNode->right != NULL)
34                     level[next].push(pNode->right);
35                 if (pNode->left != NULL)
36                     level[next].push(pNode->left);
37             }
38             if (level[current].empty()) {
39                 res.push_back(temp);
40                 temp.clear();
41                 current = 1 - current;
42                 next = 1 - next;
43             }
44         }
45         return res;
46     }
47 };

 

标签:之字形,TreeNode,offer,next,current,pNode,二叉树,push,NULL
来源: https://www.cnblogs.com/qinduanyinghua/p/11336920.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有