Q4.4 每一层的所有结点构建为一个链表
2013-08-07 15:38
239 查看
Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (i.e., if you have a tree with depth D, you’ll have D linked lists).
#include <list> typedef struct Node { int val; Node *left,*right; } vector<list<Node*>> findLevelList(Node *root) { int level = 0; vector<list<Node*>> res; list<Node*> aList; aList.push_back(root); res.push_back(); while(!res[level].empty()) { list<Node*> li; list<Node*>::iterator itr; for(itr = res[level].begin; itr!=res[level].end();itr++) { Node *n = *itr; if(n->left) li.push_back(n->left); if(n->right)li.push_back(n->right); } ++level; res.push_back(li); } return res; }
相关文章推荐
- 6-6 求单链表结点的阶乘和(15 分) 本题要求实现一个函数,求单链表L结点的阶乘和。这里默认所有结点的值非负,且题目保证结果在int范围内。
- 链表分割 编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前 给定一个链表的头指针 ListNode* pHead,请返回重新排列后的链表的头指针。注意:分割以
- 已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序。(保留所有结点,即便大小相同)
- 设计一个递归算法,删除不带头结点的单链表中所有值为x的结点
- 给定一棵二叉树,设计一个算法,创建含有某一深度上所有结点的链表(比如:若一棵树的深度为D,则会创建出D个链表)
- 设计一个递归算法,删除不带头结点的单链表L中所有值为x的结点
- 已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序。(保留所有结点,即便大小相同)
- 已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序。(保留所有结点,即便大小相同)
- <仅是自己做笔记。。。系列-4>输入一个整数和一棵二元树,从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径,打印出和与输入整数相等的所有路径
- 输入一个链表的头结点,从尾到头反过来打印每个结点的值——5
- 【C++】建立一个学生信息链表,每个结点包括:学号、姓名、成绩。【原创技术】
- 输入一个链表,反转链表后,输出链表的所有元素。
- 数据结构与算法分析笔记与总结(java实现)--链表14:删除链表中所有指定值结点问题
- 构建一个简单的链表
- 设计鲁棒性的方法:输入一个链表的头结点,逆序遍历打印该链表出来
- 有一个单项的链表,在没有头结点的情况下,只知道有一个指向结点B的指针p,假设这个结点B不是尾结点,删除该节点B。
- 输入一个链表,输出该链表中倒数第k个结点
- 《数据结构》将一个带头结点的单链表分解成两个单链表
- 输入一个单向链表,输出该链表中倒数第k个结点
- 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5