二叉树的多种 顺序遍历
来源:互联网 发布:java web异步下载文件 编辑:程序博客网 时间:2024/05/18 21:10
//中序
class Solution { /** * @param root: The root of binary tree. * @return: Inorder in vector which contains node values. */public: vector<int> a; void look(TreeNode *root) { if(root==NULL) return; look(root->left); a.push_back(root->val); look(root->right); } vector<int> inorderTraversal(TreeNode *root) { look(root); return a; // write your code here }};
//层次遍历
#include<queue>class Solution { /** * @param root: The root of binary tree. * @return: Level order a list of lists of integer */public: vector<vector<int>> a; vector<vector<int>> levelOrder(TreeNode *root) { queue<TreeNode *> q; if(root==NULL) return a; q.push(root); TreeNode *a1; a1=new TreeNode; vector<int> xx; while(!q.empty()) { xx.clear(); int x=q.size(); for(int j=0;j<x;j++) { a1=q.front(); q.pop(); int t=a1->val; xx.push_back(t); if(a1->left!=NULL) q.push(a1->left); if(a1->right!=NULL) q.push(a1->right); } a.push_back(xx); } return a; // write your code here }};
后续遍历
class Solution { /** * @param root: The root of binary tree. * @return: Postorder in vector which contains node values. */public: vector<int> a; void look(TreeNode *root) { if(root==NULL) return ; look(root->left); look(root->right); a.push_back(root->val); } vector<int> postorderTraversal(TreeNode *root) { look(root); return a; // write your code here }};
0 0
- 二叉树的多种 顺序遍历
- Java实现二叉树的多种遍历
- 二叉树的多种遍历方式
- java实现二叉树的多种遍历
- 关于二叉树的多种遍历算法
- 二叉树的遍历顺序
- 二叉树遍历顺序
- 快速写出二叉树的遍历顺序
- 顺序二叉树的建立、前中后序遍历。
- 二叉树的多种遍历——Java
- 二叉树的多种遍历方法加例题
- 【数据结构】二叉树的顺序存储结构--遍历二叉树
- 二叉树-根据已知的遍历顺序还原二叉树
- Java实现二叉树的遍历(多种方式实现前中后序的遍历)
- 简单二叉树的构造,前序遍历顺序为;中序遍历顺序为;后序遍历顺序为;
- 根据已知二叉树的遍历顺序,求另一种遍历顺序
- 顺序存储的前序遍历(二叉树)
- 顺序结构实现二叉树的建立和各种遍历
- js 进行数字运算
- 学习文本分类(一) 补充1
- jdk配置失败
- ser2net的双向通信
- Caffe2简介
- 二叉树的多种 顺序遍历
- 用插件完美解决百度云下载限制
- lintcode子树
- JS创建对象的几种方式
- 会声会影浪漫婚礼视频——美到想哭
- JavaScript算法小集(一)
- 浏览器(chrome,FF)的debug模式
- 60
- Min-Min算法 Max-Min算法