LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
题目描述
给定一个二叉树,返回它的中序 遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3 输出: [1,3,2]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
解题思路
由于中序遍历的顺序是左孩子->父节点->右孩子,所以在遍历到某节点时,先依次把所有左孩子入栈,找到最左边的子节点后,输出该节点,然后继续遍历该节点的右孩子。
代码
/**
* Definition for a binary tree node.
* 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;
stack<TreeNode*> s;
TreeNode *node = root;
while(node || s.size()){
while(node){
s.push(node);
node = node->left;
}
node = s.top();
res.push_back(node->val);
s.pop();
if(node->right)
node = node->right;
else node = NULL;
}
return res;
}
};
LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)的更多相关文章
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
- LeetCode 94:二叉树的中序遍历 Binary Tree Inorder Traversal
题目: 给定一个二叉树,返回它的中序 遍历. Given a binary tree, return the inorder traversal of its nodes' values. 示例: 输 ...
- [Swift]LeetCode94. 二叉树的中序遍历 | Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- Java实现 LeetCode 94 二叉树的中序遍历
94. 二叉树的中序遍历 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? / ...
- Leetcode 94. 二叉树的中序遍历
1.问题描述 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 2.解法一 ...
- leetcode 94二叉树的中序遍历
递归算法C++代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- 【leetcode 94. 二叉树的中序遍历】解题报告
前往二叉树的:前序,中序,后序 遍历算法 方法一:递归 vector<int> res; vector<int> inorderTraversal(TreeNode* root ...
- LeetCode 94 ——二叉树的中序遍历
1. 题目 2. 解答 2.1. 递归法 定义一个存放树中数据的向量 data,从根节点开始,如果节点不为空,那么 递归得到其左子树的数据向量 temp,将 temp 合并到 data 中去 将当前节 ...
随机推荐
- Windows 7 系统下显示文件类型的扩展名和隐藏文件
一.显示扩展名 点击开始菜单 在搜索框中输入「文件夹选项」并单击 切换到「查看」栏,取消勾选「隐藏已知文件类型的扩展名」这一项 设置完成 ps: 你也可以通过单击下图位置进行相应操作来达到同样的效果 ...
- 1 c# 获取当前正在运行的类的程序集
public static Assembly CurrentAssembly { get { return Assembly.GetExecutingAssembly(); } }
- centos8下jdk13和tomcat9的安装
首先下载JDK13和tomcat9在对应的官网上: 通过xftp传到linux服务器上的对应的目录,如/usr/local apache-tomcat-9.0.27.tar.gz ,jdk-13.0 ...
- windows 下 node 入门
node js node -v npm -v nvm v nvm list npm install * -g npm install express -g npm install -g expres ...
- hdf5文件、tqdm模块、nunique、read_csv、sort_values、astype、fillna
pandas.DataFrame.to_hdf(self, path_or_buf, key, **kwargs): Hierarchical Data Format (HDF) ,to add an ...
- Invalid character found in the request target. The valid characters are defined in RFC 7230 and RF
SpringBoot 请求参数包含 [] 特殊符号 返回400状态 //springBoot 启动类 添加 bean @Bean public TomcatServletWebServerFactor ...
- tornado框架自定义中间件过程中的一些基础技术(1)
为了检查当前请求是否在用户的权限列表中,我们需要获取uri(也就是当前链接),下列代码说明了获取的过程,也证明了python魔术方法的重要性class testHandler(RequestHandl ...
- 【vs2015发布程序】
1.选中网站右键,选择发布Web应用 2.发布目标选择自定义 3.配置文件名称 4.发布方式选择File System,选择发布的程序存放路径 5.
- 【转】分布式文件系统FastDFS架构剖析
FastDFS是一款类Google FS的开源分布式文件系统,它用纯C语言实现,支持Linux.FreeBSD.AIX等UNIX系统.它只能通过专有API对文件进行存取访问,不支持POSIX接口方式, ...
- vue环境搭建及简单接触
1.安装node环境 首先官网安装nodejs,下载地址https://nodejs.org/en/ 很多情况下,npm i 命令安装的包都是要科学上网的,或者就是国际网,下载速度很慢,不过有个淘宝镜 ...