forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
42 lines (42 loc) · 1.06 KB
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<ListNode*> listOfDepth(TreeNode* tree) {
vector<ListNode*> ans;
queue<TreeNode*> q{{tree}};
while (!q.empty()) {
ListNode* dummy = new ListNode(0);
ListNode* cur = dummy;
for (int k = q.size(); k; --k) {
TreeNode* node = q.front();
q.pop();
cur->next = new ListNode(node->val);
cur = cur->next;
if (node->left) {
q.push(node->left);
}
if (node->right) {
q.push(node->right);
}
}
ans.push_back(dummy->next);
}
return ans;
}
};