【LeetCode】Binary Tree Inorder Traversal(二叉树的中序遍历)
这道题是LeetCode里的第94道题。
题目要求:
给定一个二叉树,返回它的中序 遍历。
示例:
输入: [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) {
stack<TreeNode*>st;
TreeNode* pt;
vector<int>res;
if(root==NULL)
return res;
pt=root;
while(pt!=NULL||st.size()!=0){
while(pt!=NULL){
st.push(pt);
pt=pt->left;
}
pt=st.top();
st.pop();
res.push_back(pt->val);
pt=pt->right;
}
return res;
}
};
提交结果:
个人总结:
先左后右,推荐和前序遍历、后序遍历一起看,比较一下代码的不同。
【LeetCode】Binary Tree Inorder Traversal(二叉树的中序遍历)的更多相关文章
- [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 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...
- Leetcode94. Binary Tree Inorder Traversal二叉树的中序遍历(两种算法)
给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class So ...
- [LeetCode] 94. Binary Tree Inorder Traversal(二叉树的中序遍历) ☆☆☆
二叉树遍历(前序.中序.后序.层次.深度优先.广度优先遍历) 描述 解析 递归方案 很简单,先左孩子,输出根,再右孩子. 非递归方案 因为访问左孩子后要访问右孩子,所以需要栈这样的数据结构. 1.指针 ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- [Leetcode] Binary tree inorder traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
随机推荐
- 织梦后台上传mp4视频不显示
include/dialog/select_media.php ,约185行, 把(rm|rmvb) 改为(rm|mp4|rmvb)
- Android Google Map API使用的八个步骤
本系列教程将分为两部分,第一部分是指导用户使用Mapview控件进行编程,其中包括了如何获得Google Map API,如何使用该API进行简单的开发,如何获得用户当前所在的位置.第二部分则包括如何 ...
- [Java]Eclipse无法import “com.sun.management.OperatingSystemMXBean”的解决方法
背景: 当使用jdk的自带的API获取系统信息时,代码中import “com.sun.management.OperatingSystemMXBean”出现报错. 解决方法: Eclipse默认把这 ...
- linux下杀毒工具clamav
ClamAV 杀毒是Linux平台最受欢迎的杀毒软件,ClamAV属于免费开源产品,支持多种平台,如:Linux/Unix.MAC OS X.Windows.OpenVMS.ClamAV是基于病毒扫描 ...
- Round #322 (Div. 2) 581D Three Logos (模拟)
先枚举两个矩形,每个矩形横着放或竖着放,把一边拼起来, 如果不是拼起来有缺口就尝试用第三个矩形去补. 如果没有缺口就横着竖着枚举一下第三个矩形和合并的矩形x或y拼接. #include<bits ...
- [CV笔记]OpenCV机器学习笔记
KNN算法: 目的是分类,具体过程为,先训练,这个训练我估计只是对训练数据进行一个存储,knn测试的过程是根据测试样例找出与这个样例的距离最近的k个点,看这k个点中哪个分类所占的比例比较多,那么这个样 ...
- JAVA并发编程:相关概念及VOLATILE关键字解析
一.内存模型的相关概念 由于计算机在执行程序时都是在CPU中运行,临时数据存在主存即物理内存,数据的读取和写入都要和内存交互,CPU的运行速度远远快于内存,会大大降低程序执行的速度,于是就有了高速缓存 ...
- Problem Y: 哪一天,哪一秒?
Problem Y: 哪一天,哪一秒? Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 337 Solved: 196[Submit][Status][ ...
- Happy Equation
Source: The 10th Shandong Provincial Collegiate Programming Contest 题解: 因为2^p为偶数,所以a,x的奇偶性相同 1.当a为奇数 ...
- CPP-基础:extern"C"
简介:extern "C" 包含双重含义,从字面上即可得到:首先,被它修饰的目标是“extern”的:其次,被它修饰的目标是“C”的.让我们来详细解读这两重含义. 含义: 1.被e ...