class Solution {
public:
vector<ListNode*> listOfDepth(TreeNode* tree) {
vector<ListNode*> ans;
if (tree == NULL)
{
return ans;
}
queue<TreeNode*> q;
q.push(tree);
q.push(NULL);
int flag = 0;
ListNode* head, * index;
while (1)
{
TreeNode* p = q.front();q.pop();
if (p == NULL)
{
ans.push_back(head);
if (q.empty())
{
break;
}
flag = 0;
q.push(NULL);
continue;
}
if (!flag)
{
head = new ListNode(p->val);
index = head;
flag = 1;
}
else
{
ListNode* tmp = new ListNode(p->val);
index->next = tmp;
index = index->next;
}
if (p->left) q.push(p->left);
if (p->right) q.push(p->right);
}
return ans;
}
};
LeetCode面试题 04.03. 特定深度节点链表
最新推荐文章于 2021-10-11 00:26:17 发布