题目
给定一棵二叉树,设计一个算法,创建含有某一深度上所有节点的链表(比如,若一棵树的深度为 D,则会创建出 D 个链表)。返回一个包含所有深度的链表的数组。
示例:
输入:[1,2,3,4,5,null,7,8]
1
/ \
2 3
/ \ \
4 5 7
/
8
输出:[[1],[2,3],[4,5,7],[8]]
解答:bfs
/**
* 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*> res;
vector<ListNode*> listOfDepth(TreeNode* tree) {
if (tree == nullptr)
return res;
queue<TreeNode*> q;
q.push(tree);
while(!q.empty()) {
int sz = q.size();
ListNode* l = new ListNode(0);
ListNode* prev = l;
for(int i = 0; i < sz; ++i) {
TreeNode* t = q.front();
q.pop();
l->next = new ListNode(t->val);
l = l->next;
if(t->left)
q.push(t->left);
if (t->right)
q.push(t->right);
}
res.push_back(prev->next);
}
return res;
}
};