LeetCode:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium
题目:Binary Tree Inorder Traversal
二叉树的中序遍历,和前序、中序一样的处理方式,代码见下:
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x): val(x), left(NULL),right(NULL) {}
};
vector<int> preorderTraversal(TreeNode *root) //非递归的中序遍历(用栈实现)
{
if (NULL == root) {
return vector<int>();
}
stack<TreeNode *> tree_stack;
vector<int> tree_vector;
TreeNode *pTemp = root;
while (pTemp || !tree_stack.empty()) {
while (pTemp) {
tree_stack.push(pTemp);
pTemp = pTemp->left;
}
if (!tree_stack.empty()) {
pTemp = tree_stack.top();
tree_vector.push_back(pTemp->val);
tree_stack.pop();
pTemp = pTemp->right;
}
}
return tree_vector;
}
LeetCode:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium的更多相关文章
- 94 Binary Tree Inorder Traversal(二叉树中序遍历Medium)
题目意思:二叉树中序遍历,结果存在vector<int>中 解题思路:迭代 迭代实现: /** * Definition for a binary tree node. * struct ...
- [Leetcode] Binary tree inorder traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [Leetcode] Binary tree postorder traversal二叉树后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- 144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)
题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: class Solution { pu ...
- LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium
题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: struct TreeNode { int val; TreeNode* left; ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- LeetCode:145_Binary Tree Postorder Traversal | 二叉树后序遍历 | Hard
题目:Binary Tree Postorder Traversal 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...
随机推荐
- hello2
String username = request.getParameter("username");//获取参数值 if (username != null && ...
- DOM 节点node
DOM可以将任何HTML或XML文档描绘成一个有多层节点构成的结构,即在HTML中所有内容都是节点.文档节点是每个文档的根节点,文档节点有一个子节点,称为文档元素.每个文档只能有一个文档元素.在HTM ...
- SVD及其在推荐系统中的作用
本文先从几何意义上对奇异值分解SVD进行简单介绍,然后分析了特征值分解与奇异值分解的区别与联系,最后用python实现将SVD应用于推荐系统. 1.SVD详解 SVD(singular value d ...
- 【实习】从ubuntu迁移过来的代码,在centos上编译问题的解决汇总
目前自己的开发环境(同将来线上环境)是centos 7.我这里主要实现服务端.需要组里其他同学提供一个接口(视频编辑).公司内部自己开发环境 通常是台式ubuntu16.04.所以提供视频处理接口是在 ...
- Selenium 学习汇总
Commands (命令) Action 对当前状态进行操作 失败时,停止测试 Assertion 校验是否有产生正确的值 Element Locators 指定HTML中的某元素 Patterns ...
- c#NPOI读取excel 比interop和Microsoft.Jet.OLEDB.4.0 之类 的好的多
今天下午开始整理excel这块, 微软弄的那些库简直是个坑, 什么com注册之类的净是些报错. 在网上搜资料偶然碰见npoi ,好东西,值得使用 NPOI是指构建在POI 3.x版本之上的一个程序,N ...
- 小程序css--view标签下英文不换行,中文会自动换行
英文且没有空格不会自动换行,中文或者有空格的英文会自动换行 这种情况在css中添加 : word-break: break-all;\ 就可以解决
- magento中Model创建以及该Model对于数据库的增删改查
本文是按照magento英文文档照做与翻译的. Model层的实现是mvc框架的一个巨大的部分.它代表了你的应用的数据,或者说大多数应用没有数据是无用的.Magento的Model扮演着一个重要的角色 ...
- 20165213Java第二次实验
实验二Java面向对象程序设计 实验1 实验目的与要求: 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST 完成单元测试的学习 提交 ...
- 【Java】代理模式、反射机制-动态代理
关于代理模式和动态代理参考自:https://www.cnblogs.com/gonjan-blog/p/6685611.html 这里通过参考博客中的例子整理个人理解. 代理模式: 访问某个类的方法 ...