class Solution {
public:
vector<ListNode*> listOfDepth(TreeNode* tree) {
queue<TreeNode*> q;
q.push(tree);
vector<ListNode*> ret;
while(!q.empty()){
int size = q.size();
ListNode* head = new ListNode(0);
ListNode* p = head;
while(size--){
TreeNode* cur = q.front();
q.pop();
if(cur->left){
q.push(cur->left);
}
if(cur->right){
q.push(cur->right);
}
p->next = new ListNode(cur->val);
p = p->next;
}
ret.push_back(head->next);
delete head;
}
return ret;
}
};
面试题 04.03. 特定深度节点链表(层次遍历)
最新推荐文章于 2022-09-01 22:06:08 发布