binary-tree-inorder-traversal——二叉树中序遍历
Given a binary tree, return the inordertraversal of its nodes' values.
For example:
Given binary tree{1,#,2,3},
1
\
2
/
3
return[1,3,2].
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> res;
if(root==NULL) return res;
stack<TreeNode*> s;
TreeNode* cur=root;
while(cur||!s.empty()){
while(cur!=NULL){
s.push(cur);
cur=cur->left;
}
cur=s.top();
s.pop();
res.push_back(cur->val);
cur=cur->right;
}
return res;
} };
binary-tree-inorder-traversal——二叉树中序遍历的更多相关文章
- [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- 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 | 二叉树中序遍历 | Medium
题目:Binary Tree Inorder Traversal 二叉树的中序遍历,和前序.中序一样的处理方式,代码见下: struct TreeNode { int val; TreeNode* l ...
- 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] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- LeetCode:145_Binary Tree Postorder Traversal | 二叉树后序遍历 | Hard
题目:Binary Tree Postorder Traversal 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
随机推荐
- 秀秀的森林(forest)
秀秀的森林(forest) 题目要求树上两条不相交的链,且要求权值的和最大 性质: 1.如果某棵树上的最长链端点为x,y,则该树上任意一点z出发的最长链为max(xz,zy) 2如果两个点被连进了树里 ...
- redis学习(三)五种数据结构
Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). 1.string string类型是Redis最基 ...
- 使用 Nginx 过滤网络爬虫
现在有许多初学者学习网络爬虫,但他们不懂得控制速度,导致服务器资源浪费.通过 Nginx 的简单配置,能过滤一小部分这类爬虫. 方法一:通过 User-Agent 过滤 Nginx 参考配置如下: l ...
- jsp导出table数据excel表
<html> <head> <meta http-equiv="content-Type" content="text/html;chars ...
- Secure services with TLS ---Docker配置https
官方文档:https://docs.docker.com/ee/ucp/interlock/usage/tls/
- PowerDesigner反向生成数据库模型(MySql篇)
目录: 数据库的反向生成模型 模型的Cooment注释显示 步骤一:下载odbc驱动并进行安装: (1)下载 mysql-connector-odbc-5.3.4-win32 注意:不管电脑是32位, ...
- ofbiz16 idea 启动
1.下载gradle并安装到本地 2.idea引入gradle 3.gradle右键选择refresh,项目会重新编译并加载gradle的task 4.可以再编译一下 5.没问题的话打开,jar ap ...
- 汽车加油行驶(cogs 737)
«问题描述:给定一个N*N 的方形网格,设其左上角为起点◎,坐标为(1,1),X 轴向右为正,Y轴向下为正,每个方格边长为1,如图所示.一辆汽车从起点◎出发驶向右下角终点▲,其坐标为(N,N).在若干 ...
- Python操作MySQL[转]
本篇对于Python操作MySQL主要使用两种方式: 1.原生模块pymsql. 2.ORM框架SQLAchemy. pymsql pymsql是Python中操作MySQL的模块,其使用方法和MyS ...
- 【HDOJ5996】dingyeye loves stone(Nim游戏)
题意:dingyeye喜欢和你玩石子游戏.dingyeye有一棵n个节点的有根树,节点编号为0到n−1,根为0号节点. 游戏开始时,第i个节点上有a[i]个石子.两位玩家轮流操作,每次操作玩家可以选择 ...