剑指offer—把二叉树打印成多行
2015-10-08 21:39
429 查看
华电北风吹
天津大学认知计算与应用重点实验室
日期:2015/10/8
题目描述
从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
解析:这个跟按行打印一样的思路即可。
天津大学认知计算与应用重点实验室
日期:2015/10/8
题目描述
从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
解析:这个跟按行打印一样的思路即可。
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } }; */ class Solution { public: vector<vector<int> > Print(TreeNode* pRoot) { vector<vector<int>> result; if (pRoot == NULL) return result; vector<int> temp; TreeNode * flag = new TreeNode(NULL); queue<TreeNode *> q; q.push(pRoot); q.push(flag); while (true) { TreeNode * top = q.front(); q.pop(); if (top == flag) { result.push_back(temp); temp.clear(); q.push(top); top = q.front(); q.pop(); if (top == flag) break; } temp.push_back(top->val); if (top->left != NULL) q.push(top->left); if (top->right != NULL) q.push(top->right); } return result; } };
相关文章推荐
- 剑指offer—按之字形顺序打印二叉树
- 实现点击不同的按钮加载不同的css
- 剑指offer—对称的二叉树
- 多角度了解Javascript闭包
- Bootstrap
- jquery向列表添加新元素
- 剑指offer—二叉树的下一个结点
- header('Content-type:text/html;charset = utf-8');出现中文乱码
- 剑指offer—删除链表中重复的结点
- javascript 笔试 做一个图片轮播的脚本
- 无需Flash无需依赖任何JS库实现文本复制与剪切
- 剑指offer—链表中环的入口结点
- 对于新增节点的JS操作问题
- Merge Two Sorted Lists & Remove Nth Node From End of List
- 剑指offer—字符流中第一个不重复的字符
- 剑指offer—构建乘积数组
- 剑指offer—数组中重复的数字
- React Native入门遇到的一些问题
- 剑指offer—把字符串转换成整数
- 剑指offer—求1+2+3+...+n