File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 就是在广搜的基础上,同时带上高度,不难做,稍微繁琐了一点,自己的代码有提高的空间
2
+
3
+ class Solution {
4
+ public:
5
+ vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
6
+ vector<vector<int>> results;
7
+ vector<int> result;
8
+ if (root)
9
+ {
10
+ queue<TreeNode *> cur,next;
11
+ int level = 0;
12
+ cur.push(root);
13
+ result.push_back(root->val);
14
+ results.push_back(result);
15
+ result.clear();
16
+ while(!cur.empty())
17
+ {
18
+ while (!cur.empty())
19
+ {
20
+ TreeNode *tempNode = cur.front();
21
+ cur.pop();
22
+ if(tempNode->left)
23
+ {
24
+ next.push(tempNode->left);
25
+ result.push_back(tempNode->left->val);
26
+ }
27
+ if(tempNode->right)
28
+ {
29
+ next.push(tempNode->right);
30
+ result.push_back(tempNode->right->val);
31
+ }
32
+
33
+ }
34
+ level++;
35
+ if(result.size() > 0)
36
+ {
37
+ if(level % 2 == 0)
38
+ results.push_back(result);
39
+ else
40
+ {
41
+ reverse(result.begin(),result.end());
42
+ results.push_back(result);
43
+ }
44
+ result.clear();
45
+ swap(cur,next);
46
+ }
47
+
48
+ }
49
+ }
50
+ return results;
51
+ }
52
+ };
You can’t perform that action at this time.
0 commit comments