Skip to content

Commit 2a7adf3

Browse files
ChunelFengjunfeng.fj
and
junfeng.fj
authored
更新了lcof-cpp中[从上到下打印二叉树]中README.md内容 (doocs#336)
Co-authored-by: junfeng.fj <junfeng.fj@alibaba-inc.com>
1 parent b34dcf8 commit 2a7adf3

File tree

1 file changed

+32
-0
lines changed
  • lcof/面试题32 - I. 从上到下打印二叉树

1 file changed

+32
-0
lines changed

lcof/面试题32 - I. 从上到下打印二叉树/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,38 @@ func levelOrder(root *TreeNode) []int {
150150
}
151151
```
152152

153+
### **C++**
154+
155+
```cpp
156+
class Solution {
157+
public:
158+
vector<int> levelOrder(TreeNode* root) {
159+
vector<int> ret;
160+
if (!root) {
161+
return ret;
162+
}
163+
164+
queue<TreeNode *> q;
165+
q.push(root);
166+
167+
while (!q.empty()) {
168+
auto head = q.front();
169+
q.pop();
170+
ret.push_back(head->val);
171+
if (head->left) {
172+
q.push(head->left);
173+
}
174+
175+
if (head->right) {
176+
q.push(head->right);
177+
}
178+
}
179+
180+
return ret;
181+
}
182+
};
183+
```
184+
153185
### **...**
154186

155187
```

0 commit comments

Comments
 (0)