File tree 1 file changed +54
-0
lines changed
1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<vector<int >> zigzagLevelOrder (TreeNode* root) {
4
+ vector<vector<int >>A;
5
+ if (root==NULL ){
6
+ return A;
7
+ }
8
+
9
+ stack<TreeNode*>q1;
10
+ stack<TreeNode*>q2;
11
+
12
+ q1.push (root);
13
+
14
+ while (!q1.empty ())
15
+ {
16
+ vector<int >B;
17
+ int sz1=q1.size ();
18
+ while (sz1--){
19
+ TreeNode * node =q1.top ();
20
+ q1.pop ();
21
+ B.push_back (node->val );
22
+ if (node->left ){
23
+ q2.push (node->left );
24
+ }
25
+ if (node->right ){
26
+ q2.push (node->right );
27
+ }
28
+ }
29
+ if (B.size ()!=0 ){
30
+ A.push_back (B);
31
+ }
32
+ B.clear ();
33
+ int sz2=q2.size ();
34
+
35
+ while (sz2--){
36
+ TreeNode * node =q2.top ();
37
+ q2.pop ();
38
+ B.push_back (node->val );
39
+ if (node->right )
40
+ {
41
+ q1.push (node->right );
42
+ }
43
+ if (node->left )
44
+ {
45
+ q1.push (node->left );
46
+ }
47
+ }
48
+ if (B.size ()!=0 ){
49
+ A.push_back (B);
50
+ }
51
+ }
52
+ return A;
53
+ }
54
+ };
You can’t perform that action at this time.
0 commit comments