二叉树的中序遍历
2015-09-28 12:55
489 查看
容易 二叉树的中序遍历
39%通过
给出一棵二叉树,返回其中序遍历
您在真实的面试中是否遇到过这个题?
Yes
样例
给出二叉树
{1,#,2,3},
1 \ 2 / 3
返回
[1,3,2].
挑战
你能使用非递归算法来实现么?
标签 Expand
递归 二叉树 二叉树遍历
/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */ class Solution { /** * @param root: The root of binary tree. * @return: Inorder in vector which contains node values. */ vector<int> ret; stack<TreeNode *> tmp; public: vector<int> inorderTraversal(TreeNode *root) { // write your code here //tmp.clear(); TreeNode *s = root; while(!tmp.empty() || s){ while(s){ tmp.push(s); s = s->left; } ret.push_back(tmp.top()->val); s = tmp.top()->right; tmp.pop(); } return ret; } };
相关文章推荐
- 数据结构实验的目的、要求和评分标准
- USB设备的插入检测
- 完美的CListCtrl控件自绘
- 数据库管理系统(DBMS)的概述
- 最简单的推送--uexGetui
- java面试题二十八 从未用过的 native
- Oracle根据SQL_ID批量收集真实执行计划
- Android中自定义圆形图片的CircleImageView和RoundedImageView基本使用效果
- 二分查找
- win8系统笔记本装成win7
- uml各类图
- 插件总结
- Map---Java双列集合
- using namespace std
- Linux下Bonding配置
- jQuery的ajax async同步和异步介绍
- Linux下安装配置JDK
- 根据数组构造线段树
- 滴滴出行2016校招编程题
- 主元素