We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 0c4d100 + 5106bb0 commit 0d436daCopy full SHA for 0d436da
solution/0100-0199/0102.Binary Tree Level Order Traversal/Solution.cpp
@@ -0,0 +1,29 @@
1
+/**
2
+ * Definition for a binary tree node.
3
+ * struct TreeNode {
4
+ * int val;
5
+ * TreeNode *left;
6
+ * TreeNode *right;
7
+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8
+ * };
9
+ */
10
+class Solution {
11
+public:
12
+ vector<vector<int>> levelOrder(TreeNode* root) {
13
+ if (!root) return {};
14
+ vector<vector<int>> res;
15
+ queue<TreeNode*> q{{root}};
16
+ while (!q.empty()) {
17
+ vector<int> oneLevel;
18
+ for (int i = q.size(); i > 0; --i) {
19
+ TreeNode* t = q.front();
20
+ q.pop();
21
+ oneLevel.push_back(t->val);
22
+ if (t->left) q.push(t->left);
23
+ if (t->right) q.push(t->right);
24
+ }
25
+ res.push_back(oneLevel);
26
27
+ return res;
28
29
+};
0 commit comments