-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzhiShapeBinaryTree.cpp
More file actions
46 lines (44 loc) · 1.62 KB
/
Copy pathzhiShapeBinaryTree.cpp
File metadata and controls
46 lines (44 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,
第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。*/
/* 不使用reverse!!! 借助两个stack,分别为s1和s2。
1. 首先将根节点压入栈s1。
2. 将s1依次出栈,保存每个节点值,并依次将每个节点的左右节点压入栈s2
3. 将s2依次出栈,保存每个节点值,并依次将每个节点的右左节点压入本s1<注:这里是先压右子
节点再压左子节点>
*/
class Solution {
public:
vector<vector<int> > Print(TreeNode* pRoot) {
vector<vector<int> > result;
if (!pRoot) { return result; }
stack<TreeNode*> s1;
stack<TreeNode*> s2;
vector<int> temp;
s1.push(pRoot);
while (true) {
while (!s1.empty()) {
TreeNode* ptr = s1.top();
s1.pop();
if (!ptr) { continue; }
s2.push(ptr->left);
s2.push(ptr->right);
temp.push_back(ptr->val);
}
if (temp.empty()) { break; }
result.push_back(temp);
temp.clear();
while (!s2.empty()) {
TreeNode* ptr = s2.top();
s2.pop();
if (!ptr) { continue; }
s1.push(ptr->right);
s1.push(ptr->left);
temp.push_back(ptr->val);
}
if (temp.empty()) { break; }
result.push_back(temp);
temp.clear();
}
return result;
}
};