File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ ```
2
+ /**
3
+ * Definition for a binary tree node.
4
+ * struct TreeNode {
5
+ * int val;
6
+ * TreeNode *left;
7
+ * TreeNode *right;
8
+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
9
+ * };
10
+ */
11
+ class Solution {
12
+ public:
13
+ vector<vector<int>> levelOrder(TreeNode* root) {
14
+ vector<vector<int>> ret;
15
+ if(!root) return ret;
16
+
17
+ queue<TreeNode*> q;
18
+ q.push(root);
19
+
20
+ while(!q.empty())
21
+ {
22
+ int n = q.size();
23
+ vector<int> tmp;
24
+
25
+ while(n>0)
26
+ {
27
+ TreeNode *node = q.front();
28
+ q.pop();
29
+ n--;
30
+
31
+ tmp.push_back(node->val);
32
+ if(node->left)
33
+ q.push(node->left);
34
+ if(node->right)
35
+ q.push(node->right);
36
+
37
+ }
38
+
39
+ ret.push_back(tmp);
40
+ }
41
+
42
+ return ret;
43
+ }
44
+ };
45
+ ```
You can’t perform that action at this time.
0 commit comments